@@ -30,10 +30,11 @@ import {
3030 HasLength ,
3131 HasProperties ,
3232 IsDefined ,
33+ IsString ,
3334 validate ,
3435} from ' @modulify/validator'
3536
36- const violations = validate ({
37+ const violations = await validate ({
3738 form: {
3839 nickname: ' ' ,
3940 password: ' ' ,
@@ -42,21 +43,109 @@ const violations = validate({
4243 form: [
4344 IsDefined (),
4445 HasProperties ({
45- nickname: HasLength ({ min: 4 }),
46- password: HasLength ({ min: 6 }),
46+ nickname: IsString . That ( HasLength ({ min: 4 }) ),
47+ password: IsString . That ( HasLength ({ min: 6 }) ),
4748 }),
4849 ],
4950})) /* [{
50- by: '@modulify/validator/HasLength ',
51+ by: '@modulify/validator/IsString ',
5152 value: '',
5253 path: ['form', 'nickname'],
5354 reason: 'min',
5455 meta: 4,
5556}, {
56- by: '@modulify/validator/HasLength ',
57+ by: '@modulify/validator/IsString ',
5758 value: '',
5859 path: ['form', 'password'],
5960 reason: 'min',
6061 meta: 6,
6162}] */
62- ```
63+ ```
64+
65+ or (for synchronous validation):
66+
67+ ``` typescript
68+ const violations = validate .sync ({
69+ form: {
70+ nickname: ' ' ,
71+ password: ' ' ,
72+ },
73+ }, HasProperties ({
74+ form: [
75+ IsDefined (),
76+ HasProperties ({
77+ nickname: IsString .That (HasLength ({ min: 4 })),
78+ password: IsString .That (HasLength ({ min: 6 })),
79+ }),
80+ ],
81+ }))
82+ ```
83+
84+ ## Exported types
85+
86+ * ` Violation ` – an object that contains information about a value – why it violates certain one or more constraints;
87+ includes following fields:
88+ * ` value ` – value that violates something;
89+ * ` path ` – path to the value, an empty array for scalar values and represents full path to the value in a complex
90+ object;
91+ * ` violates ` – indicator of the violated constraint;
92+ * ` reason ` – indicator of the reason why the constraint is violated;
93+ * ` meta ` – some data to describe the reason – what exactly the boundaries were not met;
94+ ``` typescript
95+ import type { Violation } from ' @modulify/validator/types'
96+ ```
97+ * ` Predicate ` – function that accepts a value and returns ` true ` or ` false ` ; logical unit that is used for checking
98+ multiple things: type or if the value satisfies certain criteria; accepts generic argument ` T ` to specify
99+ the type of the value, if predicate returns ` true ` ;
100+ ``` typescript
101+ import type { Predicate } from ' @modulify/validator/types'
102+ ```
103+ * ` Assertion ` – extension of the ` Predicate ` type that includes:
104+ * ` fqn ` – field – some predefined name that will be used as a value for the ` violates ` field of ` Violation ` ;
105+ * ` bail ` – field – flag that interrupts further validation if the assertion fails;
106+ * ` reason ` – field, optional – string or symbol that is used to indicate, why assertion has failed;
107+ always added to a violation object, if present;
108+ * ` meta ` – field, optional – some metadata to use in further analysis; always added to a violation object, if present;
109+ * ` That ` – method – used to extend assertion with other assertions;
110+ * ` also ` – field – readonly array of other assertions that was attached by ` That ` method;
111+ ``` typescript
112+ import type { Assertion } from ' @modulify/validator/types'
113+ ```
114+
115+ ## Exported members
116+
117+ * ` validate ` – function that accepts a value for validation as the first argument, constraints as the second,
118+ and path to a value as the third (that is optional and used mostly for internal purposes, as validation is recursive);
119+ includes method ` sync ` that has the same arguments set but performs validation synchronously and throws error when
120+ finds an asynchronous constraint;
121+
122+ * ` Assert ` – creates assertion from logical predicate:
123+ ``` typescript
124+ const IsSomething = Assert (isSomething , {
125+ fqn: ' Some fqn' ,
126+ bail: true ,
127+ })
128+ ```
129+ Arguments:
130+ * Logical predicate
131+ * Options, that includes ` fqn ` , ` bail ` , ` reason ` (optional), and ` meta ` (optional);
132+ * ` HasLength ` – checks length property of the specified string or array; can be configured with options:
133+ * ` exact ` – if the length should be exactly equal the specified value;
134+ * ` max ` – if the length should be equal or less than the specified value;
135+ * ` min ` – if the length should be equal or greater than the specified value;
136+ * ` bail ` – set this to true if you need to interrupt further validation if the assertion fails;
137+ * ` IsBoolean ` – checks if the value is ** boolean** ; interrupts further validation if fails;
138+ * ` IsDate ` – checks if the value is Date ** object** ; interrupts further validation if fails;
139+ * ` IsDefined ` – checks if the value is ** not undefined** ; interrupts further validation if fails;
140+ * ` IsEmail ` – checks if the value is a ** valid email** ; interrupts further validation if fails;
141+ * ` IsNull ` – checks if the value is ** null** ; interrupts further validation if fails;
142+ * ` IsNumber ` – checks if the value is ** number** ; interrupts further validation if fails;
143+ * ` IsString ` – checks if the value is ** string** ; interrupts further validation if fails;
144+ * ` IsSymbol ` – checks if the value is a ** symbol** ; interrupts further validation if fails;
145+ * ` OneOf ` – checks if the value equal to one of the specified values; can be configured with:
146+ * ` equalTo ` – predicate f(a, b) that checks if two values are equal or not;
147+ by default the strict ` === ` comparison is used
148+ * ` bail ` – set this to true if you need to interrupt further validation if the assertion fails;
149+
150+ * ` Each ` – a runner that runs validation for each element in array;
151+ * ` HasProperties ` – a runner that runs object's structure check.
0 commit comments