0.3.3-beta.3 • Published 2 years ago

@acai/validator v0.3.3-beta.3

Weekly downloads
4
License
BSD-3-Clause
Repository
github
Last release
2 years ago

Açai Validator Module

https://gitlab.com/acaijs/validator.git https://www.npmjs.com/package/@acai/validator https://www.npmjs.com/package/@acai/validator https://www.npmjs.com/package/@acai/validator

A customizable validator for the Açaí framework. You can easily add your own rules and extend the capabilities of our validator.

Usage

import { addRule } from "@acai/validator";

// first thing we need is to extend the base Validator class
class RegisterValidator extends Validator {
  protected getSchema() {
    return {
      email: ["required", "email"],
      password: ["required", "confirmed"],
    };
  }
}

// now we get the fields to be validated
const fields = {
  email: "not an email",
  password: "not confirmed password",
};

const validation = new RegisterValidator(fields);
const errors = validation.errors;

// this will be filled if the validation failed
if (errors) {
  console.warn(errors);
}

Custom rules

import { addRule } from "@acai/validator";

addRule("password", {
	/** Validation to check if value should pass */
	onValidate	? (data: {value: unknown, key: string, fields: Record<string, unknown>, args?: string[], rules: string[]}) => {
		return /* your validation here */
	}
	/** Message to be returned in case of validation error */
	onError		? (data: {value: unknown, key: string, fields: Record<string, unknown>, args?: string[], rules: string[]}) => {
		return `${key} is not a valid password`;
	}
	/** Alters values to other rules */
	onMask		? (data: {value: unknown, key: string, fields: Record<string, unknown>, args?: string[], rules: string[]}) => {
		return /* new masked value here */;
	}
});

Rules

Type validation rules

nameargsdescription
arraycontent type, such as: string, number, etc...Check if field is an array
objectcontent type, such as: string, number, etc...Check if field is an object
booleanno argumentsCheck if field is an boolean
stringno argumentsCheck if field is an string
number/floatforce to allow strings that can be convertable to numberCheck if value is a valid number and if force is passed, maskes field into an actual number if is not
integerforce to allow strings that can be convertable to numberCheck if value is a valid integer and if force is passed, maskes field into an actual integer if is not

Composite validation rules

Those are rules that adapt to other rules present in the same field

nameargsdescription
includeslist of keywords to lookCheck if the field includes the keywords present, if it's an object, will look for the keys, if it's an array, will look for the values. If it's a string or a number, will look for the pattern
minminimal value to be requiredIf the field is an object or array, will look the quantity of keys. If it's a string, will look for character quantity. And if it's a number, is bigger than the passed argument
maxmax value to be requiredIf the field is an object or array, will look the quantity of keys. If it's a string, will look for character quantity. And if it's a number, is smaller than the passed argument

General validation rules

nameargsdescription
requiredno argumentsField cannot be undefined
confirmedreference fieldLooks for a field with the suffix _confirmation that has the same value
uuidno argumentsField must be a valid uuid4
truthyno argumentsField must be a javascript true comparison (1, "any string value, true, etc...)
emailno argumentsField must be a valid email
regexregex patternField must match the provided regex pattern
dateoptional format for dateString that contains a valid date format
0.3.3-beta.3

2 years ago

0.3.3-beta.2

3 years ago

0.3.2-beta.1

3 years ago

0.3.2-beta.0

3 years ago

0.3.2-alpha

3 years ago

0.3.2-alpha2

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago