joi-fun
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

joi-fun

Validate function that lapping hapijs/joi creator.

Setup

$ npm install joi-fun

Usage

Call createValidator() with function that return joi schema object. Validate function returns joi's ValidationResult object.

If you want to know you more schema definition, see joi's API Reference.

const validator = createValidator<ValueType>(
  (joi) => joi.object().keys({
    username: joi.string().alphanum().min(3).max(30).required(),
    password: joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
    access_token: joi.alternatives().try(joi.number(), joi.string()),
    birthyear: joi.number().integer().min(1900).max(2013),
    email: joi.string().email()
  })
  .with('username', 'birthyear')
  .without('password', 'access_token')
);

const result = validator({ username: 'abc', birthyear: 1994 });

createValidator() also accepts literal schema definition.

const validator = createValidator<ValueType>(['key', 5, { a: true, b: [/^a/, 'boom'] }]);
const result = validator({ a: true, b: 'apple' })

Readme

Keywords

Package Sidebar

Install

npm i joi-fun

Weekly Downloads

7

Version

0.0.4

License

MIT

Last publish

Collaborators

  • akira-miyake