Skip to content

Commit

Permalink
docs: Updated README.md, added repository and homepage fields to pack…
Browse files Browse the repository at this point in the history
…age.json, some texts fixes in tests
  • Loading branch information
cmath10 committed Feb 5, 2024
1 parent 416c506 commit ad5fbc8
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
122 changes: 122 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,126 @@ const violations = validator.validate({
reason: 'min',
meta: 6,
}] */
```

### Constraints

Constraints provide information of how the value should be validated.

Available from the box:

* `Collection` – used for validating objects' structure;
* `Each` – used for validating arrays' elements; applies specified constraints to each element of an array;
* `Exists` – used for checking if a value is defined; useful for finding missing keys;
* `Length` – used for checking arrays' and string's length, available settings (all optional) are:
* `exact` – `number`, array or string should have exactly specified count of elements or characters;
* `max` – `number`, maximum elements in array or maximum characters in string;
* `min` – `number`, minimum elements in array or minimum characters in string;
* `OneOf` – used for restricting which values can be used.

There is no any basic constraint class to extend, but they should follow signature
described in `types/index.d.ts` – `Constraint`.

### Validators

Validators provide validation logic that relies on information provided by constraints.

There is no any basic validator class to extend, but they should follow signature
described in `types/index.d.ts` – `ConstraintValidator`.

### Provider

Provider is used to bind constraints with their validators, provides a validator for a constraint.

All providers should follow signature described in `types/index.d.ts` – `Provider`.

This feature is responsible for extending validation capabilities. Custom provider can be passed into
`createValidator` function or `override` method of `Validator` instance.

There is a built-in provider – `ProviderChain`. It allows to "chain" providers – if
suitable validator was not found in currently used provider, it will try to find it in previous provider that was
overridden by `override` method.

```typescript
import type {
Constraint,
ConstraintValidator,
ConstraintViolation,
Key,
Provider,
} from '@modulify/validator'

import {
ProviderChain,
createValidator,
} from '@modulify/validator'

class Email implements Constraint {
public readonly name = '@app/validator/Email'

toViolation (value: unknown, path: Key[]): ConstraintViolation {
return {
by: this.name,
value,
path,
}
}
}

class EmailValidator implements ConstraintValidator {
private readonly _constraint: Email

constructor (constraint: Email) {
this._constraint = constraint
}

validate (value: unknown, path?: Key[]): ConstraintViolation | null {
if (!(typeof value === 'string') || !/\S+@\S+\.\S+/.test(value)) {
return this._constraint.toViolation(value, path)
}

return null
}
}
```

then

```typescript
const provider = new ProviderChain(new class implements Provider {
get (constraint: Constraint) {
return constraint instanceof Email ? new EmailValidator(constraint) : null
}

override (provider: Provider): Provider {
return new ProviderChain(provider, this)
}
})
```

or

```typescript
const provider = new class implements Provider {
get (constraint: Constraint) {
return constraint instanceof Email ? new EmailValidator(constraint) : null
}

override (provider: Provider): Provider {
return new ProviderChain(provider, this)
}
}
```

and then

```typescript
const validator = createValidator(provider)
```

or

```typescript
const validator = createValidator()
const overridden = validator.override(provider) // it creates new validator instance, so validator !== overridden
```
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
"contributors": [
"Zaitsev Kirill <[email protected]>"
],
"homepage": "https://github.com/modulify/validator",
"repository": {
"type": "git",
"url": "https://github.com/modulify/validator.git"
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
Expand Down
4 changes: 2 additions & 2 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('validates synchronously', () => {
})

describe('Each', () => {
test('checks entries in array', () => {
test('checks elements in array', () => {
expect(validator.validate([
{ name: '' },
{ name: 'longEnough' },
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('validates asynchronously', () => {
})

describe('Each', () => {
test('checks entries in array', async () => {
test('checks elements in array', async () => {
expect(await validator.validate([
{ name: '' },
{ name: 'longEnough' },
Expand Down

0 comments on commit ad5fbc8

Please sign in to comment.