Attributes
Includes Deno configuration
Repository
Current version released
2 years ago
A Savory class validator for Deno.
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. $constraint1 will be replaced by first argument. $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));
// User { email: 'myemail@mydomain.com' }
// ---------- //
// 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