DEV Community

alguerocode
alguerocode

Posted on

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 });
Enter fullscreen mode Exit fullscreen mode
  • 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
  }
});
Enter fullscreen mode Exit fullscreen mode

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
});
Enter fullscreen mode Exit fullscreen mode

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'
});
Enter fullscreen mode Exit fullscreen mode

📝 License

Copyright © 2021 salah alhashmi.

This project is MIT licensed.

Top comments (3)

Collapse
 
alguercode profile image
alguerocode

welcome

Collapse
 
alguercode profile image
alguerocode

good

Collapse
 
alguercode profile image
alguerocode

nice