@5alid/schema

model validation utility, similar to mongoose `schema` but for general usage, mainly to validate objects for noSQL databases

Usage no npm install needed!

<script type="module">
  import 5alidSchema from 'https://cdn.skypack.dev/@5alid/schema';
</script>

README

Installation

npm npm i @5alid/schema yarn yarn add @5alid/schema

Usage

import createSchema from '@5alidz/schema';

const Person = createSchema({
  name: {
    type: 'string',
    minLength: 5,
    maxLength: 20,
  },
  age: {
    type: 'number',
    min: 13,
    max: 120,
  },
  pets: {
    type: 'array',
    optional: true,
    of: {
      type: 'object',
      shape: {
        name: {
          type: 'string',
        },
        animal: {
          type: 'string',
        },
      },
    },
  },
});

const errors = Person.validate({
  name: 'John Doe',
  age: 42,
  pets: [
    { name: 'fluffy', animal: 'dog' },
    { name: 'pirate', animal: 'parrot' },
  ],
});

console.log(errors); // -> null

const errors2 = Person.validate({
  name: true,
  age: '42',
});

console.log(errors2); // -> [ { key: 'name', message: 'expected string but received "boolean"' }, { key: 'age', message: 'expected number but received "string"' }]