DEV Community

Cover image for Schema - Your data ruler validator
Miguel Ramos
Miguel Ramos

Posted on

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

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: "ramos@websublime.dev"
});

expect(validation.properties?.age.hasError).toBeFalsy();
expect(validation.properties?.email.hasError).toBeFalsy();
Enter fullscreen mode Exit fullscreen mode

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

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

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! ๐ŸŽ‰

Top comments (0)