New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

indicative-parser

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

indicative-parser

Rules and messages schema parser for Indicative

  • 7.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.8K
increased by1.2%
Maintainers
1
Weekly downloads
 
Created
Source

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: [],
    }],
  },
}
PropertyDescription
typeThe type of the node
rulesAn 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.

PropertyDescription
typeThe type of the node
rulesAn array of parsed rules on the object node itself
childrenAn 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.

PropertyDescription
typeThe type of the node
rulesAn array of parsed rules on the object node itself
eachSet 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.

Keywords

FAQs

Package last updated on 01 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc