Skip to content

Commit 8177e55

Browse files
committed
docs: Updated README.md according to the last changes of the logic
1 parent ba8de41 commit 8177e55

File tree

6 files changed

+108
-15
lines changed

6 files changed

+108
-15
lines changed

README.md

Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@
8181
},
8282
"keywords": [
8383
"validate",
84-
"validator"
84+
"validator",
85+
"ES2017"
8586
],
8687
"contributors": [
8788
"Zaitsev Kirill <[email protected]>"

src/assertions/Assert.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ const delegate = <T>(p: Predicate<T>): Predicate<T> => {
1111
export const Assert = <
1212
T = unknown,
1313
M = unknown
14-
>(predicate: Predicate<T>, meta: Meta<M>): Assertion<T, M> => {
14+
>(predicate: Predicate<T>, options: Meta<M>): Assertion<T, M> => {
1515
const extender = (asserts: Assertion[] = []): Pick<Assertion<T, M>, 'That' | 'also'> => {
1616
return {
1717
That (...asserts: Assertion[]) {
18-
return Object.assign(delegate(predicate), meta, extender(asserts)) as Assertion<T, M>
18+
return Object.assign(delegate(predicate), options, extender(asserts)) as Assertion<T, M>
1919
},
2020

2121
get also (): Assertion[] {
@@ -24,5 +24,5 @@ export const Assert = <
2424
} as Assertion<T, M>
2525
}
2626

27-
return Object.assign(delegate(predicate), meta, extender())
27+
return Object.assign(delegate(predicate), options, extender())
2828
}

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import type {
88

99
import check from '@/assertions/check'
1010

11+
export * from '@/assertions'
12+
export * from '@/runners'
13+
1114
const isBatch = (c: Constraint): c is ValidationRunner => {
1215
return 'run' in c
1316
}
@@ -65,7 +68,7 @@ const _sync = <T>(
6568
const v = 'That' in c ? check(c, value, [...path]) : c(value, [...path])
6669

6770
if (v instanceof Promise) {
68-
throw new Error('Found asynchronous constraint validator ' + c.fqn)
71+
throw new Error('Found asynchronous constraint validator ' + String(c.fqn))
6972
} else if (v) {
7073
violations.push(v)
7174

tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"compilerOptions": {
33
"esModuleInterop": true,
4-
"module": "esnext",
5-
"moduleResolution": "node",
4+
"module": "ESNext",
5+
"moduleResolution": "Node",
66
"paths": {
77
"@/*": ["./src/*"],
88
"~types": ["./types/index.d.ts"],
99
"~types/*": ["./types"]
1010
},
1111
"resolveJsonModule": true,
12-
"target": "es6"
12+
"target": "ES2017"
1313
},
1414
"include": [
1515
"src/**/*.ts",

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type MaybePromise<V> = V | Promise<V>
1010

1111
export type Predicate<T = unknown> = (value: unknown) => value is T
1212
export type Meta<T> = {
13-
fqn: string,
13+
fqn: string | symbol,
1414
bail: boolean;
1515
reason?: string | symbol;
1616
meta?: T;

0 commit comments

Comments
 (0)