1.0.30 • Published 1 month ago
@budarin/validate.ts v1.0.30
validate.ts
Entities validation utils.
Installation
yarn add @budarin/validate
Usage
import type { LikeExtended, FieldsValidators, ValidateEntity } from '@budarin/validate';
import { validateEntity, isIntegerInRange, isStringWithLength } from '@budarin/validate.ts';
type User = {
name: string;
age: number;
};
function getUser(obj: LikeExtended<User>): User {
return {
name: obj.name,
age: obj.age,
};
}
const userFields: FieldsValidators = {
name: {
validators: [[isStringWithLength(2, 50), 'The name must be a string between 2 and 50 characters long']],
required: true,
},
age: {
validators: [[isIntegerInRange(1, 100), 'The age must be an integer from 1 to 100']],
required: true,
},
};
const validateUser: ValidateEntity<User> = (data: unknown) => validateEntity(data, userFields, getUser, 'User');
const user = {
name: 'Ivan',
age: 30,
hair: 'brown',
};
const validation = validateUser(user);
if (v.error) {
console.log(validation.error.message); // => 'The name must be a string between 2 and 50 characters long'
}
console.log(validation.result); // => { name: 'Ivan', age: 30 }
List of utils
- hexColorvalidator
- validateEntity
- isUndefinedOr
- isInteger
- isIntegerInRange
- mustBeInt
- mustBeUndefinedOrInt
- isHexColor
- mustBeHexString
- hexColorValidator
- isStringWithLength
- isISODateTimeString
- stringHasWrongLength
- isBoolean
- mustBeUndefinedOrBoolean
- isObject
Exported types
export type Validator = (...args: any[]) => boolean;
export type ValidateEntity<T> = (data: unknown) => ResultOrError<T>;
export type EntityGetter<T> = (obj: LikeExtended<T>) => DeepReadonly<T>;
export type FieldsValidators = {
[key: string]: {
validators: [Validator, string][];
required?: boolean;
};
};
export type LikePartial<T> = Partial<T>;
export type LikeExtended<T> = T & Partial<Record<string | number, unknown>>;
export type Like<T> = T | (T & Partial<Record<string | number, unknown>>) | Partial<T>;
export type AnyObject = Record<string, unknown>;
License
MIT
1.0.29
1 month ago
1.0.30
1 month ago
1.0.28
1 month ago
1.0.27
1 month ago
1.0.26
2 months ago
1.0.25
2 months ago
1.0.23
2 months ago
1.0.22
2 months ago
1.0.21
2 months ago
1.0.20
3 months ago
1.0.19
4 months ago
1.0.18
4 months ago
1.0.17
4 months ago
1.0.16
4 months ago
1.0.15
5 months ago
1.0.9
5 months ago
1.0.11
5 months ago
1.0.10
5 months ago
1.0.14
5 months ago
1.0.13
5 months ago
1.0.12
5 months ago
1.0.8
5 months ago
1.0.7
5 months ago
1.0.6
5 months ago
1.0.5
5 months ago
1.0.4
5 months ago
1.0.3
5 months ago
1.0.2
5 months ago
1.0.1
5 months ago
1.0.0
5 months ago