1.0.0 • Published 4 years ago

@crinkles/schematiq v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Schematiq - JavaScript object schematiqs

npm.io Node version NPM Downloads Minified size License: MIT

A lightweight library that allows for object schema validation.

Object validation

For object validation one defines rules for each property of the object. Combined, rules define the validation schema. Each rule must contain a type. Other settings are optional.

type Rule = {
  type: 'string' | 'boolean' | 'number'; // type error
  required?: boolean; // required error
  regexp?: RegExp; // format error, only when type = 'string'
  custom?: (value, obj) => boolean; // custom error; if custom === false, it gives an error
};

type Schema = {
  [key: string]: Rule; // also for nested properties, e.g. "nested.property"
};

when

An object can be validated by using the validate function of schematiq. It returns an object indicating which properties of the object have errors. It also indicates the type of error, unless you set a custom error message.

import validator from 'schematiq';

const errors = validator(obj, schema);
// { "nested.property": "type" | "required" | "format" | "other" | "my custom message" }