feathers-hooks-yup
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

feathers-hooks-yup

Feathers hook utility for schema validation and sanitization using Yup. Error messages are converted to web/mobile friendly formats, and optionally translated for clarity or internationalization.

Supports both JS and Typescript.

The reason I decided to do this, because I found that using Yup clientside with react is the recommended approach. but I did not want to rewrite the rules in JOI or any other validation framework that are already available. this hook allows me to simply share the validations from the clientside.

Build Status Coverage Status

Code Example

import * as Yup from "yup";
import {Hook, HookContext} from '@feathersjs/feathers';
import {BadRequest} from '@feathersjs/errors';
 
const yupSchema = Yup.object({
  email: Yup.string()
    .email("invalid email")
    .required("email required"),
  retypeEmail: Yup.string()
    .oneOf([Yup.ref('email'), undefined], 'email not equal')
    .required("must enter email again"),
  password: Yup.string()
    .required('must enter password')
    .min(8, 'password must be 8 letters')
    .matches(/[a-zA-Z0-9]/, 'error ...'),
  acceptedTerms: Yup.boolean()
    .required("Required")
    .oneOf([true], "must accept terms"),
});
export default yupSchema;
import yupValidationHook from 'feathers-hook-yup';
import yupSchema from './yupSchema.js';
const yupOptions = {abortEarly: false};
export.before = {
  create: [ yupValidationHook(yupSchema, yupOptions, translationMethod) ],
  update: [ yupValidationHook(yupSchema, yupOptions, translationMethod) ],
  patch: [ yupValidationHook(yupSchema, yupOptions, translationMethod) ]
};
 

(3) Internationalize or clarify Joi error messages.

Translation method example:

//some translating logic...
function i18n(str) { return str; } // internationalization
const translationMethod = (formattedErrors) => {
        for(const errorKey in formattedErrors){
            formattedErrors[errorKey] = i18n(formattedErrors[errorKey]);
        }
        return formattedErrors;
    }
 
export.before = {
  create: [ yupValidationHook(yupSchema, yupOptions, translationMethod) ],
};

Motivation

Installation

Install Nodejs.

Run npm install feathers-hooks-yup --save in your project folder.
or
Run yarn add feathers-hooks-yup in your project folder.
You can then require the utilities.

API Reference

To do.

Tests

To do. npm test to run tests.

npm run cover to run tests plus coverage.

Contributors

Credit

  • logicwind for taking their readme as an example, and for the inspiration to set up this package.

License

MIT. See LICENSE.

Package Sidebar

Install

npm i feathers-hooks-yup

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

11.8 kB

Total Files

7

Last publish

Collaborators

  • vasilevich