0.5.2 • Published 11 months ago
@softkit/validation v0.5.2
Validation Wrapper around class-validator
We need this wrapper because of few reasons:
- class-validator is a nice library, but it's not actively supported anymore, and we consider forking it and maintaining it by ourselves.
- there are plenty of alternatives for class-validator, but it is the most convenient one that we've ever used, and a lot of other libraries are using it as well. So having a single validator for everything is a good idea.
- class-validator stopped supporting dynamic validation schema, and in this library we implemented it for our needs and with our structures
- we also tight it with nestjs-i18n, so we can easily override default translations
- also we're providing better transformers for types like date and boolean, default class-validator transformers are not working properly with these and other types
- providing additional useful decorators like
@Match
, to check if one field match another, and other commonly requested decorators that missed in class-validator, or proper @IsEmailLocalized validator and they are localized
Validation rules for methods, can be found here
Installation
yarn add @softkit/validation
Usage
Usage is as simple as with plain class-validator
Dynamic validator usage
- This example will throw the appropriate exception that will be handled by our filter and return RFC7807 error response if value doesn't match validator schema
import { IsEnumValidatorDefinition, validateAndThrow } from '@softkit/validators';
validateAndThrow(
IsEnumValidatorDefinition,
'fieldName',
'fieldValue',
['enumValue1', 'enumValue2'],
);
- This example will throw exception if value doesn't match constraint
import { MatchesRegexpValidatorDefinition, validateAndThrow } from '@softkit/validators';
const constraint = /^-?(?!0\d)\d+$/;
validateAndThrow(
MatchesRegexpValidatorDefinition,
params.key,
value as string,
constraint,
i18nString('validation.INTEGER'),
);
-- If you don't need to throw exception immediately you can use validateAndReturnError
method, that returns ValidationError
object, that you can use later
import { IsEnumValidatorDefinition, validateAndThrow } from '@softkit/validators';
const error = validateAndReturnError(
IsEnumValidatorDefinition,
'fieldName',
'fieldValue',
['enumValue1', 'enumValue2'],
);
console.log(error);
0.5.0
11 months ago
0.4.0
12 months ago
0.5.2
11 months ago
0.5.1
11 months ago
0.3.7
1 year ago
0.3.6
1 year ago
0.3.5
1 year ago
0.3.4
2 years ago
0.3.3
2 years ago
0.3.2
2 years ago
0.3.1
2 years ago
0.3.0
2 years ago
0.2.0
2 years ago
0.1.3
2 years ago
0.1.2
2 years ago
0.1.1
2 years ago
0.1.0
2 years ago
0.0.5
2 years ago
0.0.4
2 years ago
0.0.3
2 years ago
0.0.2
2 years ago
0.0.1
2 years ago