2.0.1 • Published 9 months ago

vamlid v2.0.1

Weekly downloads
12
License
MIT
Repository
github
Last release
9 months ago

vamlid

A simple way to validate an object against a pure javascript schema.

Installation

npm install --save vamlid

Usage (sync)

import { sync as vamlid } from 'vamlid';

function validateUser (data) {
  const schema = {
    email: [
      value => !value && 'is required',
      value => value && !value.includes('@') && 'should contain an @ symbol'
    ],

    password: [
      value => !value && 'is required',
      value => value && value.length < 5 && 'should be greater than 5 characters'
    ]
  };

  return vamlid(schema, data);
}

const result = validateUser({
  email: 'wrong',
  notHere: 'test'
});

console.log(result)
/*
{
  fields: {
    email: [
      'should contain an @ symbol'
    ],
    password: [
      'is required'
    ],
    notHere: [
      'is not a valid key'
    ]
  },
  messages: [
    'notHere is not a valid key'
  ]
}
*/

Usage (async)

import { async as vamlid } from 'vamlid';

function validateUser (data) {
  const schema = {
    email: [
      value => someDbProvider
        .findAll({ email: value })
        .then((results) => results.length > 0 && 'already taken')
      value => !value && 'is required',
      value => value && !value.includes('@') && 'should contain an @ symbol'
    ],

    password: [
      value => !value && 'is required',
      value => value && value.length < 5 && 'should be greater than 5 characters'
    ]
  };

  return vamlid(schema, data);
}

const result = await validateUser({
  email: 'wrong',
});

console.log(result)
/*
{
  fields: {
    email: [
      'already taken',
      'should contain an @ symbol'
    ],
    password: [
      'is required'
    ],
    notHere: [
      'is not a valid key'
    ]
  },
  messages: [
    'notHere is not a valid key'
  ]
}
*/