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…


This content originally appeared on DEV Community and was authored by alguerocode

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: 'test@test.com'
});

📝 License

Copyright © 2021 salah alhashmi.

This project is MIT licensed.


This content originally appeared on DEV Community and was authored by alguerocode


Print Share Comment Cite Upload Translate Updates
APA

alguerocode | Sciencx (2021-10-13T14:28:44+00:00) powerful javascript and simple schema builder for value parsing and validation.. Retrieved from https://www.scien.cx/2021/10/13/powerful-javascript-and-simple-schema-builder-for-value-parsing-and-validation/

MLA
" » powerful javascript and simple schema builder for value parsing and validation.." alguerocode | Sciencx - Wednesday October 13, 2021, https://www.scien.cx/2021/10/13/powerful-javascript-and-simple-schema-builder-for-value-parsing-and-validation/
HARVARD
alguerocode | Sciencx Wednesday October 13, 2021 » powerful javascript and simple schema builder for value parsing and validation.., viewed ,<https://www.scien.cx/2021/10/13/powerful-javascript-and-simple-schema-builder-for-value-parsing-and-validation/>
VANCOUVER
alguerocode | Sciencx - » powerful javascript and simple schema builder for value parsing and validation.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/13/powerful-javascript-and-simple-schema-builder-for-value-parsing-and-validation/
CHICAGO
" » powerful javascript and simple schema builder for value parsing and validation.." alguerocode | Sciencx - Accessed . https://www.scien.cx/2021/10/13/powerful-javascript-and-simple-schema-builder-for-value-parsing-and-validation/
IEEE
" » powerful javascript and simple schema builder for value parsing and validation.." alguerocode | Sciencx [Online]. Available: https://www.scien.cx/2021/10/13/powerful-javascript-and-simple-schema-builder-for-value-parsing-and-validation/. [Accessed: ]
rf:citation
» powerful javascript and simple schema builder for value parsing and validation. | alguerocode | Sciencx | https://www.scien.cx/2021/10/13/powerful-javascript-and-simple-schema-builder-for-value-parsing-and-validation/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.