Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 25, 2022 11:25 pm GMT

Schema - Your data ruler validator

Ol again , happy to introduce a new package Schema, a condition ruler validation for your data.

It is design to check if your data is compliant with the rules you define. It can be used as native form validator in any platform/framework as a raw, native validator. It will be integrated on other tool in the future for forms agnostic validation. It as ZERO dependencies and is developed in typescript. Feel free to contribute also.

Features

  • Rules for all native types (String, Number, Date, Boolean)
  • Support custom rules
  • Support object types and nested objects
  • Support on arrays of any type
  • Async checks

Install

You can install as:

yarn add @websublime/schema

Express steps

Define your schema of rules, then just check your data type if it is valid with the rules defined.

const schemaObject = ObjectType<{ age: number; email: string }>({  age: NumberType().min(18),  email: StringType().isEmail()});let validation = await schemaObject.check({  age: 19,  email: "[email protected]"});expect(validation.properties?.age.hasError).toBeFalsy();expect(validation.properties?.email.hasError).toBeFalsy();

Or just simple native types:

const str = StringType().minLength(5);expect((await str.check("abcde")).hasError).toBeFalsy();expect((await str.check("abcd")).hasError).toBeTruthy();const validationSchema = NumberType().max(10);expect((await validationSchema.check(9)).hasError).toBeFalsy();expect((await validationSchema.check(11)).hasError).toBeTruthy();

Each native type as some rules already defined like:

String:

  • containsLetter (check if value contains only letters)
  • containsUppercaseLetter (check if value is uppercase)
  • containsLowercaseLetter (check if value is lowercase)
  • containsLetterOnly (check if value contains letters only)
  • containsNumber (check if value constains numbers)
  • isOneOf (check if is one of the types included)
  • isEmail (check if is valid email)
  • isURL (check if is valid url)
  • isHex (check if is a hex value)
  • pattern (test a reg expression)
  • rangeLength (check if value is between minimum and maximum length)
  • minLength (check if value as minimum length)
  • maxLength (check if value is less then maximum length)

And many more types. Also you can define custom rules or dependencies between rules.

A example more like real world:

type FormRegister = {  password: string|null;  email: string|null;  firstName: string|null;  lastName: string|null;  repeat: string|null;};const formSchema = ObjectType<FormRegister>({    email: StringType().isEmail('Please provide a valid email').isRequired('Email is required'),    firstName: StringType().isRequired('First name is required'),    lastName: StringType().isRequired('Last name is required'),    password: StringType().minLength(8, 'Password should have minimum 8 characters').isRequired('Password is required'),    repeat: StringType().minLength(8, 'Password should have minimum 8 characters').isRequired('Repeat password is required').addRule({      errorMessage: 'Password confirmation not matching',      validationFn: (value: string, parent: FormRegister) => {        const { password = null } = parent || {};        return value == password;      }    })  });

Feel free to contribute!

Soon a github page with complete documentation and api. More to know, dig a bit testings. There are good examples there.

Links

Github link: https://github.com/websublime/schema

Happy coding!


Original Link: https://dev.to/miguelramos/schema-your-data-ruler-validator-bjj

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