1.1.1 • Published 2 years ago

@gauravcodes/is-validator v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

is-validator

A zero dependency, light weight runtime validator and a schema generator with the final schema as a JSON object which could be easily shared and stored

Guide

Generate a Schema

const PersonSchema = {
    name: is.string({ minLength: 2, maxLength: 10 }),
    email: is.string({ isEmail: true, minLength: 5 }),
    age: is.number({ moreThan: 18 }),
};

This will return a JSON configuration of below shape

{
    "name": {
        "type": "string",
        "rules": {
            "minLength": 2,
            "maxLength": 10
        }
    },
    "email": {
        "type": "string",
        "rules": {
            "isEmail": true,
            "minLength": 5
        }
    },
    "age": {
        "type": "number",
        "rules": {
            "moreThan": 18
        }
    }
}

Now, we can share this schema with backend (with the help of NPM module) or store it in a DB. We can also infer the type from the schema with the help of exported type InferType

type Person = InferType<typeof PersonSchema>;

const person: Person = {
    name: 'Gaurav Thakur',
    email: 'gthakur581@gmail.com',
    age: 21,
};

Screenshot 2022-10-31 at 1 11 33 PM

We can also validate schema against the data in runtime

import { is, validateSchema } from 'is-validator';
import type { InferType } from 'is-validator';

const PersonSchema = {
    name: is.string({ minLength: 2, maxLength: 10 }),
    email: is.string({ isEmail: true, minLength: 5 }),
    age: is.number({ moreThan: 18 }),
};

type Person = InferType<typeof PersonSchema>;

const person: Person = {
    name: 'A',
    email: 'gthakur581@gmail.com',
    age: 10,
};

const result = validateSchema(PersonSchema, person);
console.log(result);

output:

image

Instead of silently returning the error, we could also throw an error by enabling shouldThrowError property

const result = validateSchema(PersonSchema, person, { shouldThrowError: true });

Inbuild Rules:

  1. is.string()

    • minLength
    • maxLength
    • isEmail
    • isURL
    • regex
    • endsWith
    • startsWith
  2. is.number()

    • isPositive
    • isNegative
    • lessThan
    • moreThan
  3. is.boolean()

    • isTruly
    • isFalsy

Todo

  • Add support for more data type e.g. boolean, enums etc
  • Add support for more rules
    • Add support for making properties optional
  • Add support for initializing the schema object when received schema from another source
1.1.1

2 years ago

1.1.0

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago