Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 13, 2021 02:28 pm GMT

powerful javascript and simple schema builder for value parsing and validation.

volder

link:volder
don't forget to leave a star in the repo

volder is a powerful JavaScript schema builder for value parsing and validation. Define a schema and validate values, volder schema are extremely simple to use, it has custom error messages, custom types, nested schemas.

Contents

Installation

npm install --save volder

Usage

You can create and validate volder schema objects by:

import Volder from 'volder';const person = new Volder({  name: {    type: String,    min: 4,    trim: true,    required: true  },  age: {    type: Number,    max: 100  }});const [isValidPerson, errors] = person.validate({ name: 'lion', age: 23 });
  • return isValidPerson true if an object are valid otherwise false
  • if there are error or something wrong return errors object with his option name otherwise return empty object {}
  • throw an error if validate function paramatere other than object
  • all types: String, Number, Boolean, Object, Array, volderSchema, null - null mean everything -

Custom error messages

You Can Define you custom error messages by:

const person = new Volder({  name: {    type: [String, "person name shoulde a string"],    min: [4, "must at least 4 characters"] ,    required: [true, "name is important"]  },  age: {    type: [Number, "your age can not be a string"],    max: [100, "age at most 100 years"]  },  other:  {    type: [null, "'other' can be anything than object and array"]    avoid:  [Object, Array],    required:false  }});

Custom type validator

You Can Define you custom types by adding a validator functions that returns a boolean:

import isEmail from 'package';import isValidPassword from 'package';const user = new Volder({  username: String, // use this trick by just add type as option value  email: {    type: [isEmail, 'Email is invalid'],    max: 100  },  password: isValidPassword});

Nested schemas

You Can Define Nested volder schemas by:

import Volder from 'volder';const person = new Volder({ name: String, age: Number });const user = new Volder({  email: { type: String, trim: true },  person: schema1});const [isValid, errors] = person.validate({  person: { name: 'lion', age: 23 },  email: '[email protected]'});

License

Copyright 2021 salah alhashmi.

This project is MIT licensed.


Original Link: https://dev.to/alguercode/powerful-javascript-and-simple-schema-builder-for-value-parsing-and-validation-4agi

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To