Skip to content
The schema parser for Indicative
Branch: develop
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
.github
docs
example
src
test
.editorconfig
.gitignore
.npmrc
CONTRIBUTING.md
LICENSE.md
README.md
config.json
index.ts
japaFile.js
package.json
tsconfig.json
tslint.json
typedoc.js

README.md

Indicative Parser

Converts indicative rules and messages schema to a tree

circleci-image npm-image license-image

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.

Table of contents

Why Indicative needs a parser?

If you look at the Indicative schema, it is very concise and developer friendly. However, the same schema needs to be parsed to execute the validation rules.

{
  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 pre-compile the schema (later cache it) and generate a tree, which is easier to reason about and then generated optimized functions from the parsed tree.

Usage

Install the package from npm registry as follows:

npm i indicative-parser@alpha

# yarn
yarn add indicative-parser@alpha

and then use it as follows:

import { rulesParser } from 'indicative-parser'

rulesParser({
  username: 'required',
  'account.type': 'required|in:email,social'
})

Above code outputs the following tree.

{
  "username": {
    "type": "literal",
    "rules": [
      {
        "name": "required",
        "args": []
      }
    ]
  },
  "account": {
    "rules": [],
    "type": "object",
    "children": {
      "type": {
        "type": "literal",
        "rules": [
          {
            "name": "required",
            "args": []
          },
          {
            "name": "in",
            "args": [
              "email",
              "social"
            ]
          }
        ]
      }
    }
  }
}

Maintainers

Harminder virk

You can’t perform that action at this time.