@ezier/validate
TypeScript icon, indicating that this package has built-in type declarations

0.2.3 • Public • Published

Ezier Validator logo

Ezier Validator

An ezier validator for nodejs.

npm bundle size npm NPM npm

Why?

This validator allows you to validate your objects with eze. Nothing more, nothing less.

Also used in the Fronvo server

Installing

npm i @ezier/validate

Documentation

Documentation for the Ezier Validator can be found at https://ezier-project.github.io/validate/.

Examples

Validate a string's length:

import { StringSchema } from '@ezier/validate';

const schema = new StringSchema({
    value: {
        minLength: 5,
        maxLength: 20
    }
});

const result = schema.validate({
    value: 'some string'
});

Validating manually with regex:

import { v4 } from 'uuid';

const uuidSchema = new StringSchema({
    uuid: {
        regex: /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/
    },
});

uuidSchema.validate({ uuid: v4() });

Or with provided types:

import { v4 } from 'uuid';

const uuidSchema = new StringSchema({
    uuid: {
        type: 'uuid'
    },
});

uuidSchema.validate({ uuid: v4() });

With support for optional fields:

const schema = new StringSchema({
    provideThis: {
        minLength: 8,
        maxLength: 60,
        type: 'email',
    },

    orThis: {
        minLength: 8,
        maxLength: 30,
        regex: /^[a-zA-Z0-9]+$/,
        optional: true
    },
});

schema.validate({
    provideThis: 'anemail@gmail.com'
});

Getting error info:

for (const errorObjectIndex in result) {
    const errorObject = result[Number(errorObjectIndex)];

    console.log(`[${errorObject.name}]: ${errorObject.message}`);
}

More examples located here

Made by Shadofer with joy.

Package Sidebar

Install

npm i @ezier/validate

Weekly Downloads

5

Version

0.2.3

License

MIT

Unpacked Size

12.5 kB

Total Files

5

Last publish

Collaborators

  • shadofer