A Savory class validator for Deno.
Available decorators
Decorator | Description |
---|---|
Type validation decorators | |
@IsBoolean() |
Checks if a value is a boolean. |
@IsString() |
Checks if the string is a string. |
@IsNumber(options: IsNumberOptions) |
Checks if the value is a number. |
@IsBigInt() |
Checks if the value is a big int. |
@IsArray() |
Checks if the value is an array |
@IsObject() |
Checks if the value is an object |
@IsSymbol() |
Checks if the value is a Symbol |
@IsUndefined() |
Checks if the value is undefined |
Number validation decorators | |
@GreaterOrEqual(num: number) |
Checks if the value is greater than or equal to the specified number. |
@Greater(num: number) |
Checks if the value is greater than the specified number. |
@LowerOrEqual(num: number) |
Checks if the value is lower than or equal to the specified number. |
@Lower(num: number) |
Checks if the value is lower than the specified number. |
String validation decorators | |
@LengthGreaterOrEqual(num: number) |
Checks if the length is greater than or equal to the specified number. |
@LengthGreater(num: number) |
Checks if the length is greater than the specified number. |
@LengthLowerOrEqual(num: number) |
Checks if the length is lower than or equal to the specified number. |
@LengthLower(num: number) |
Checks if the length is lower than the specified number. |
@IsRegex(regex: RegExp) |
Checks if the string match the given RegExp. |
@IsEmail() |
Checks if the string is an email. |
@IsPhoneNumber(country: string) |
Checks if the string is a local phone number for a country. Country must be ISO 3166 c.a.d 2 letter code. Example: FR, US, UK, DE |
@IsInternationalPhoneNumber() |
Checks if the string is an international phone number with extension and with or without leading 0. Example: +330XXXXXXXXX or +33XXXXXXXXX |
Object validation decorators | |
@Nested(class: Class) |
Checks if the property is a valid “instance” of Class. This means that all Validators of Class will be checked on the given object. The property can either be a plain object or a class instance |
Usage
Create a Class using TypeScript Decorators and run validateObject
.
If you need more built-in validators, please open an issue or create merge request
import {
createValidator,
IsEmail, // this is a decorator (PascalCase)
isEmail, // this is a check function, called behavior (camelCase)
IsString,
LengthLowerOrEqual,
validateObject,
} from 'https://deno.land/x/validatte/mod.ts';
// Custom validator with custom error message containing argument. <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>t</mi><mn>1</mn><mi>w</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>e</mi><mi>p</mi><mi>l</mi><mi>a</mi><mi>c</mi><mi>e</mi><mi>d</mi><mi>b</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>r</mi><mi>s</mi><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi mathvariant="normal">.</mi></mrow><annotation encoding="application/x-tex">constraint1 will be replaced by first argument. </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">co</span><span class="mord mathnormal">n</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">ain</span><span class="mord mathnormal">t</span><span class="mord">1</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span><span class="mord mathnormal">b</span><span class="mord mathnormal">ere</span><span class="mord mathnormal" style="margin-right:0.01968em;">pl</span><span class="mord mathnormal">a</span><span class="mord mathnormal">ce</span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal">rs</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">gu</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord">.</span></span></span></span>constraint2 by second etc...
const IsWhiteListedEmailDomain = createValidator(
(whiteListedDomain: string) => {
return (prop) => {
if (isEmail(prop)) {
return false;
}
return prop.indexOf(`@${whiteListedDomain}`) !== -1;
};
},
'Email shoud be from $constraint1 domain',
);
class User {
@IsString()
@IsEmail()
@IsWhiteListedEmailDomain('mydomain.com')
email!: string;
}
// ---------- //
// Valid
const user = new User();
user.email = 'myemail@mydomain.com';
console.log(validateObject(user, User));
// []
// ---------- //
// Invalid
const failingUser = new User();
failingUser.email = 'wrongemail@email.com';
console.log(validateObject(failingUser, User));
/*
[
{
property: 'email',
errorMessage: `Email shoud be from mydomain.com domain`,
constraints: ['mydomain.com'],
},
];
*/
Come with us on this awesome journey
We always welcome contributors, feel free to submit a new feature or report a bug on our Github Repository and join our discord
License
MIT