Schema parser for Indicative
npm install indicative-parser
[![circleci-image]][circleci-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]
Indicative parser pre-compiles the Indicative schema to a recursive tree of nodes. Each node is given one of the following types.
- object: Node with one or more nested children.
- array: Node with one or more index or wildcard based nested children.
- literal: The leaf nodes.
Do note, that the literal type is not equal to literal values in Javascript. For parser, the literal nodes are nodes with no leaf.
- Why Indicative needs a parser?
- Usage
``js`
{
username: 'required',
'account.type': 'required|in:email,social'
}
One way is to loop over the schema object keys, split them by . and then inline execute the validations for each field. This process is very straight forward, but will have performance issues.
Instead, we parse the schema into a tree. The tree is later converted to an array of top level functions that are highly optimized for performance.
`sh
npm i indicative-parser
and then use it as follows:
`js
import { rulesParser } from 'indicative-parser'rulesParser({
username: 'required',
'account.type': 'required|in:email,social'
})
`Above code outputs the following tree.
`json
{
"username": {
"type": "literal",
"rules": [
{
"name": "required",
"args": []
}
]
},
"account": {
"rules": [],
"type": "object",
"children": {
"type": {
"type": "literal",
"rules": [
{
"name": "required",
"args": []
},
{
"name": "in",
"args": [
"email",
"social"
]
}
]
}
}
}
}
``[circleci-image]: https://img.shields.io/circleci/project/github/poppinss/indicative-parser/master.svg?style=for-the-badge&logo=circleci
[circleci-url]: https://circleci.com/gh/poppinss/indicative-parser "circleci"
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: "typescript"
[npm-image]: https://img.shields.io/npm/v/indicative-parser.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/indicative-parser "npm"
[license-image]: https://img.shields.io/npm/l/indicative-parser?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md "license"