JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4735
  • Score
    100M100P100Q133912F
  • License MIT

Rules and messages schema parser for Indicative

Package Exports

  • indicative-parser

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (indicative-parser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

UsageWhy?FeaturesExamplesDocumentation


Parser

Indicative parser parses the user defined schema object to a tree of nodes. Using this, the end user can always define their rules as a flat object and it's the job of the parser to expand it to a nested object.

Usage

Install the package from npm

npm i indicative-parser

# yarn user
yarn add indicative-parser

and then usage it as follows

const { rulesParser, messagesParser } = require('indicative-parser')

console.log(rulesParser({
  'client_id': 'required',
  'settings.key': 'required',
  'users.*.email': 'required|email'
}))

// messages parser
console.log(messagesParser({
  'users.*.email.required': 'Email is required',
  'users.*.email.email': 'Invalid email address'
}))

The parser tree emits three types of nodes, which are explained below:

Literal type

The literal type is the reference to the final property or leaf in the tree. A literal node doesn't have any children.

{
  username: 'required'
}

Output

{
  username: {
    type: 'literal',
    rules: [{
      name: 'required',
      args: [],
    }],
  },
}
Property Description
type The type of the node
rules An array of parsed rules

Object type

The object type defines an object, which will always have one or more children.

Following is an example of object node.

{
  'user.profile': 'required'
}

Output

{
  user: {
    type: 'object',
    rules: [],
    children: {
      profile: {
        rules: [
          {
            name: 'required',
            args: [],
          },
        ],
        type: 'literal',
      },
    },
  },
}

In the above code example, you can see that the properties before the dot . notation is marked as type = object and last property username is considered a literal. There is no limit to the depth of the object.

Property Description
type The type of the node
rules An array of parsed rules on the object node itself
children An object of nested named children

Array type

The array type detects the array expressions as shown below.

{
  'users.*.username': 'required'
}

Output

{
  users: {
    type: 'array',
    rules: [],
    each: {
      '*': {
        rules: [],
        children: {
          username: {
            type: 'literal',
            rules: [
              {
                name: 'required',
                args: [],
              },
            ],
          },
        },
      },
    },
  },
}

A node is considered as an array when it has * or numeric expressions. The each property contains a sub object for each defined index with nested children.

Property Description
type The type of the node
rules An array of parsed rules on the object node itself
each Set of children for each defined index.

What's next?

The output of parser is used by compiler to create a top level function, which executes the validations on runtime data object.