0.1.0 • Published 6 years ago

maccabee v0.1.0

Weekly downloads
1,540
License
MIT
Repository
-
Last release
6 years ago

Maccabee

Maccabee is a validation library for Javascript

Install

yarn add maccabee

or

npm i maccabee -S

How to use

Using Maccabee validatorFactory & built in validators

  const {
    validatorFactory,
    validators: {
      values: { isString, isBoolean, isNumber },
      general: { nullable },
    },
  } = require('maccabee');

  const userCreateValidation = validatorFactory({
    check: {
      id: [isString].map(nullable),
      firstName: [isString],
      lastName: [isString],
      age: [isNumber].map(nullable),
      admin: [isBoolean],
    },
  });

  const validUser = {
    firstName: 'John',
    lastName: 'McValidUser',
    admin: true,
  };

  const invalidUser = {
    firstName: 'Invalid',
    lastName: 'Invalidface',
    admin: 'false',
    age: 'a literal ferry',
  };

  try {
    const res = await userCreateValidation(validUser, {});
    console.log(JSON.stringify(res, null, 2));
    /*
    {
      "firstName": "John",
      "lastName": "McValidface",
      "admin": true
    }
    */
  } catch (ex) {
    console.log(JSON.stringify(ex, null, 2));
  }

  try {
    const res = await userCreateValidation(invalidUser, {});
    console.log(JSON.stringify(res, null, 2));
  } catch (ex) {
    console.log(JSON.stringify(ex, null, 2));
    /*
    {
      "name": "ValidationError",
      "data": {
        "age": [
          {
            "expected": "a number",
            "received": "not a number",
            "key": "age"
          }
        ],
        "admin": [
          {
            "expected": "to be a bool",
            "received": "false",
            "key": "admin"
          }
        ]
      }
    }
    */
  }
0.1.0

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago