2.0.1 • Published 4 years ago
validation-api v2.0.1
validation-api
TypeScript Validation Api, based on decorators.
Usage
import { Validate, ValidationError, IsBoolean } from 'validation-api';
@Validate()
class TestDomain {
@IsBoolean()
public booleanValue: boolean;
constructor(entity: any) {
this.booleanValue = entity.booleanValue;
}
}
try {
const success = new TestDomain({ booleanValue: false });
const fail = new TestDomain({ booleanValue: 1 });
} catch (error) {
if (error instanceof ValidationError) console.error(JSON.stringify(error));
else throw error;
}
Property decorator
All decorator methods can take argument relevant to interface IPropDecoratorOption
interface IPropDecoratorOption {
group?: string[];
message?: string;
}
Decorators IsNumber, IsPositiveNumber, IsPositiveOrZeroNumber can take extended options
interface INumberPropDecorationOption extends IPropDecoratorOption {
min?: number;
max?: number;
}
Decorator IsMemberOf gets array of possible values
export interface IMemberOfPropDecorationOption extends IPropDecoratorOption {
array: any[];
}
Decorators
- Required
- IsArray
- IsBoolean
- IsEmail
- IsMemberOf
- IsNumber
- IsPhone
- IsPositiveNumber
- IsPositiveOrZeroNumber
- IsString
- NotEmpty
- NotEmptyString
Abstract class
Complex validation
import {
AbstractValidated,
Validate,
ValidationError,
Required,
IsBoolean,
NotEmpty,
IsPositiveNumber,
NotEmptyString,
} from 'validation-api';
@Validate()
class Person extends AbstractValidated {
@Required()
@NotEmptyString({ message: 'Name is required for person' })
public name: string;
@IsPositiveNumber({ message: 'Person age must be a positive number' })
public age: number;
@IsBoolean()
public ready: boolean;
constructor(entity: any) {
super(entity);
this.name = entity.name;
if (entity.age) {
this.age = entity.age;
} // If because setting to undefined throws a Validation error is not presented
if (entity.ready) {
this.ready = entity.ready;
} // See above
}
}
try {
const success = new Person({ name: 'Ivan', age: 24, ready: true });
const fail = new Person({ name: '', age: 0, ready: 1 });
} catch (error) {
if (error instanceof ValidationError) {
console.error(error.cause);
} else {
throw error;
}
}
This example provides output in stderr:
[
{
constraint: 'NotEmptyString',
message: 'Name is required for person',
property: 'name',
value: '',
},
{
constraint: 'IsPositiveNumber',
message: 'Person age must be a positive number',
property: 'age',
value: 0,
},
{ constraint: 'IsBoolean', message: 'ready is not a Boolean', property: 'ready', value: 1 },
];
2.0.1
4 years ago
2.0.0
4 years ago
1.2.2
4 years ago
1.2.1
5 years ago
1.1.0
5 years ago
1.0.0
5 years ago
0.3.0
5 years ago
0.1.32
6 years ago
0.1.31
6 years ago
0.1.30
6 years ago
0.1.29
6 years ago
0.1.28
6 years ago
0.1.27
6 years ago
0.1.23
6 years ago
0.1.22
6 years ago
0.1.21
6 years ago
0.1.20
6 years ago
0.1.19
6 years ago
0.1.18
6 years ago
0.1.17
6 years ago
0.1.16
6 years ago
0.1.15
6 years ago
0.1.14
6 years ago
0.1.13
6 years ago
0.1.12
6 years ago
0.1.11
6 years ago
0.1.10
6 years ago
0.1.9
6 years ago
0.1.8
6 years ago
0.1.7
6 years ago
0.1.6
6 years ago
0.1.5
6 years ago
0.1.4
6 years ago
0.1.3
6 years ago
0.1.2
6 years ago
0.0.1
6 years ago