Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shalimov/egoist-js
Set of validators designed with functional approach in mind
https://github.com/shalimov/egoist-js
Last synced: about 1 month ago
JSON representation
Set of validators designed with functional approach in mind
- Host: GitHub
- URL: https://github.com/shalimov/egoist-js
- Owner: Shalimov
- Created: 2018-04-20T15:39:08.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T01:22:02.000Z (about 2 years ago)
- Last Synced: 2024-08-09T15:30:31.718Z (5 months ago)
- Language: JavaScript
- Homepage: https://shalimov.github.io/egoist-js/module-ego.html
- Size: 1.98 MB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Validation
## Small size, no deps, best validation experience!
Designed with functional programming in mind
***
Look at [Egoist-JS](https://shalimov.github.io/egoist-js) pages## Getting Started
- `yarn add egoist-js` or `npm i egoist-js`
- to use version without deps `import * as ego from 'egoist-js`
- to work with uncompiled version you can `import * as ego from 'egoist-js/lib'````javascript
import { spec, validate, validateAll, validators } from 'egoist-js'const {
any,
string,
number,
shape,
} = validatorsconst userModelDetailedSpec = spec.of({
name: spec.flow(
string.isString,
string.match(/^[A-Z][a-z]+\s[A-Z][a-z]+$/),
string.isNotEmpty,
any.required,
),
age: spec.flow(
number.isNumber,
number.ge(18),
any.required,
)
})const userModelSpec = spec.compose(
spec.flow(any.requied),
userModelDetailedSpec
)// you can create separate function to validate user
const validateUser = validate(userModelSpec)
// then use itconsole.log(validateUser(null))
// [{ message: 'value is required', args: undefined, value: null, path: [] }]// OR
const someUserData = {
name: 'Igor Shalimov',
age: 10
}console.log(validate(userModelSpec, someUserData))
// [{ message: 'age should be greater or equal than 18', args: 18, value: 10, path: ['age'] }]const adultUserSpec2 = spec.compose(
spec.designate('user', spec.flow(any.required)),
userModelDetailedSpec
)validate(adultUserSpec2, null)
// [{ message: 'user is required', args: undefined, value: null, path: [] }]```