https://github.com/michal-wrzosek/schemat
Schemat - simple runtime schema validator
https://github.com/michal-wrzosek/schemat
javascript joi package schema typescript validate validation validator yup
Last synced: 4 months ago
JSON representation
Schemat - simple runtime schema validator
- Host: GitHub
- URL: https://github.com/michal-wrzosek/schemat
- Owner: michal-wrzosek
- License: mit
- Created: 2019-09-27T11:44:31.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T18:45:14.000Z (over 2 years ago)
- Last Synced: 2025-01-21T20:48:42.726Z (5 months ago)
- Topics: javascript, joi, package, schema, typescript, validate, validation, validator, yup
- Language: JavaScript
- Homepage:
- Size: 669 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# schemat - simple runtime schema validator
 [](https://travis-ci.com/michal-wrzosek/schemat)### How to install:
```
npm i schemat
```### How to use
Validate objects with simple functions and receive error messages if any:
```typescript
import { createValidator } from 'schemat';const validator = createValidator({
a: (data: any) => (data === 'A' ? undefined : '"a" should be "A"'),
likeA: (data: any, siblingsData: any) => siblingsData && siblingsData.a === data ? undefined : '"likeA" should be equal to param "a"',
nested: createValidator({
c: (data: any) => (data === 'C' ? undefined : '"nested.c" should be "C"'),
}),
});// No errror messages here:
// errorMessages => undefined
const errorMessages = validator({
a: 'A',
likeA: 'A',
nested: {
c: 'C',
},
});// wrong b and nested.c params:
// errorMessages => {
// b: '"likeA" should be equal to param "a"',
// nested: {
// c: '"nested.c" should be "C"',
// },
// };
const errorMessages = validator({
a: 'A',
likeA: 'wrong!!!',
nested: {
c: 'wrong!!!',
},
});
```You can also pass an array of validators in your schema. Validator will run validators one by one and will stop and return first error, if any:
```typescript
import { createValidator, ValidatorType } from 'schemat';enum ERRORS {
VALIDATION_ERROR_REQUIRED_FIELD = 'VALIDATION_ERROR_REQUIRED_FIELD',
VALIDATION_ERROR_NOT_A_STRING = 'VALIDATION_ERROR_NOT_A_STRING',
VALIDATION_ERROR_INVALID_EMAIL = 'VALIDATION_ERROR_INVALID_EMAIL',
}const isRequiredValidator: ValidatorType = (data: any) =>
typeof data === 'undefined' ? ERRORS.VALIDATION_ERROR_REQUIRED_FIELD : undefined;
const stringValidator: ValidatorType = (data: any) =>
typeof data === 'undefined'
? undefined
: typeof data !== 'string'
? ERRORS.VALIDATION_ERROR_NOT_A_STRING
: undefined;
const emailValidator: ValidatorType = (data: any) =>
typeof data === 'undefined'
? undefined
: typeof data === 'string' && /^\S+@\S+$/.test(data)
? undefined
: ERRORS.VALIDATION_ERROR_INVALID_EMAIL;const validator = createValidator({
optionalEmail: emailValidator,
requiredEmail: [isRequiredValidator, emailValidator],
optionalString: stringValidator,
requiredString: [isRequiredValidator, stringValidator],
});
```---
This package was bootstrapped with [typescript-lib-boilerplate](https://github.com/michal-wrzosek/typescript-lib-boilerplate)