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 - npm Package Compare versions

Comparing version 7.1.2 to 7.1.3

8

build/index.d.ts

@@ -0,1 +1,9 @@

/**
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import * as t from './src/Types';

@@ -2,0 +10,0 @@ export { rulesParser, messagesParser } from './src/main';

"use strict";
/**
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __importStar = (this && this.__importStar) || function (mod) {

@@ -3,0 +11,0 @@ if (mod && mod.__esModule) return mod;

@@ -0,1 +1,5 @@

/**
* Parsed rule is created by parsing a single rule defination
* like `required` or `min:4`
*/
export declare type ParsedRule = {

@@ -5,2 +9,11 @@ name: string;

};
/**
* Array node defines the fields which uses one of the following
* array expressions.
*
* 1. users.*.username
* 2. users.*
* 3. users.0.username
* 4. users.0
*/
export declare type SchemaNodeArray = {

@@ -16,2 +29,8 @@ type: 'array';

};
/**
* Object node defines the fields which uses one of the following
* object expressions.
*
* 1. user.username
*/
export declare type SchemaNodeObject = {

@@ -22,2 +41,6 @@ type: 'object';

};
/**
* Literal are leaf nodes inside the tree. A literal can exist
* on an array or object or direct leafs of a flat tree
*/
export declare type SchemaNodeLiteral = {

@@ -27,9 +50,27 @@ type: 'literal';

};
/**
* Shape of the schema defined by the end user. In the later version
* we may drop the `string` based rules
*/
export declare type Schema = {
[field: string]: string | ParsedRule[];
};
/**
* The shape of schema after parser parses it
*/
export declare type ParsedSchema = {
[field: string]: SchemaNodeArray | SchemaNodeLiteral | SchemaNodeObject;
};
/**
* Shape of a single validation message. The functions are evaluated at runtime
*/
export declare type Message = string | ((field: string, validation: string, args: any[]) => string);
/**
* ------------------------------------------------------------------------
* Types for the declarative schema
* ------------------------------------------------------------------------
*/
/**
* Typed string
*/
export declare type OpaqueString = string & {

@@ -46,2 +87,5 @@ readonly __opaque__: 'string';

};
/**
* Typed number
*/
export declare type OpaqueNumber = number & {

@@ -58,2 +102,5 @@ readonly __opaque__: 'number';

};
/**
* Typed boolean
*/
export declare type OpaqueBoolean = boolean & {

@@ -70,2 +117,5 @@ readonly __opaque__: 'boolean';

};
/**
* Typed object
*/
export declare type ObjectTypeDeclaration<T extends TypedSchema> = {

@@ -83,2 +133,5 @@ t: {

};
/**
* Typed array
*/
export declare type ArrayTypeDeclaration<T extends DictTypeDeclarations> = {

@@ -92,2 +145,5 @@ t: T['t'][];

};
/**
* Typed tuple
*/
export declare type TupleTypeDeclaration<T extends DictTypeDeclarations[]> = {

@@ -105,7 +161,19 @@ t: {

};
/**
* Union of literal and object only schema types
*/
export declare type DictTypeDeclarations = StringTypeDeclaration | OptionalStringTypeDeclaration | BooleanTypeDeclaration | OptionalBooleanTypeDeclaration | NumberTypeDeclaration | OptionalNumberTypeDeclaration | ObjectTypeDeclaration<TypedSchema> | OptionalObjectTypeDeclaration<TypedSchema>;
/**
* A union of types the types schema accepts
*/
export declare type TypeDeclarations = DictTypeDeclarations | ArrayTypeDeclaration<any> | OptionalArrayTypeDeclaration<any> | TupleTypeDeclaration<any> | OptionalTupleTypeDeclaration<any>;
/**
* Shape of typed schema
*/
export declare type TypedSchema = {
[key: string]: TypeDeclarations;
};
/**
* Shape of typed schema after getting parsed
*/
export declare type ParsedTypedSchema<T extends TypedSchema> = {

@@ -117,11 +185,29 @@ schema: ParsedSchema;

};
/**
* ------------------------
* Shape of messages
* ------------------------
*/
/**
* Shape of user defined messages schema
*/
export declare type Messages = {
[field: string]: Message;
};
/**
* Shape of parsed messages for a given node
*/
export declare type ParsedRulesMessages = {
[rule: string]: Message;
};
/**
* Parsed messages tree
*/
export declare type ParsedFieldsMessages = {
[field: string]: ParsedRulesMessages;
};
/**
* Final tree for messages. The `rules` object has flat list of messages
* for a given rule. However, `fields` are scoped inside a field name.
*/
export declare type ParsedMessages = {

@@ -128,0 +214,0 @@ fields: ParsedFieldsMessages;

"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
import { Schema, Messages, TypedSchema, ParsedSchema, ParsedMessages, ParsedTypedSchema } from './Contracts';
/**
* Parses the schema object to a tree of parsed schema. The
* output is optimized for executing validation rules.
*
* @example
* ```
* parser({
* 'users.*.username': 'required'
* })
*
* // output
*
* {
* users: {
* type: 'array',
* rules: [],
* each: {
* '*': {
* rules: [],
* children: {
* username: {
* type: 'literal',
* rules: [{
* name: 'required',
* args: []
* }]
* }
* }
* }
* }
* }
* }
* ```
*/
export declare function rulesParser(schema: ParsedTypedSchema<TypedSchema> | Schema): ParsedSchema;
/**
* Parses an object of messages to [[ParsedMessages]] list. The messages list
* is simpler than rules tree, since compiler can use the schema tree to find
* the appropriate messages from the flat list of messages.
*
* @example
* ```
* parser({
* 'users.*.username.required': 'Username is required'
* })
*
* // output
*
* {
* fields: {
* 'users.*.username': {
* required: 'Username is required'
* }
* },
* rules: {},
* }
*/
export declare function messagesParser(schema: Messages): ParsedMessages;
"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -11,2 +19,7 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

}
/**
* Updates rules on the given node. If node is missing, then a literal node is
* created automatically. Literal nodes can later transform into `object` and
* `array` nodes.
*/
function setLiteral(source, key, rules) {

@@ -18,2 +31,10 @@ const item = (source[key] || { type: 'literal' });

}
/**
* Creates/updates literal node to an object node. Since `object` node
* properties are different from `literal` node, we need to set those
* properties (if missing).
*
* If node already exists and is an array node, then this method will raise an
* exception
*/
function setObject(source, key) {

@@ -29,2 +50,10 @@ if (source[key] && source[key].type === 'array') {

}
/**
* Creates/updates literal node to an array node. Since `array` node
* properties are different from `literal` node, we need to set those
* properties (if missing).
*
* If node already exists and is an object node, then this method will raise an
* exception
*/
function setArray(source, key, index) {

@@ -41,8 +70,28 @@ if (source[key] && source[key].type === 'object') {

}
/**
* Parses field tokens recursively to a [[ParsedSchema]] tree
*/
function parseFieldForRules(tokens, rules, out, index = 0) {
const token = tokens[index++];
/**
* Finding if we are on the last item. Last item defines
* the rules for the current node inside the tree
*/
const isLast = tokens.length === index;
/**
* Indexed array have `digits` like `users.0.username`
*/
const isIndexedArray = /^\d+$/.test(tokens[index]);
/**
* Is upcoming token an array
*/
const isArray = tokens[index] === '*' || isIndexedArray;
/**
* Last item was marked as array, since current token is a `*`
* or has defined index
*/
if (token === '*' || /^\d+$/.test(token)) {
/**
* Last item must update rules for each item for the array
*/
if (isLast) {

@@ -52,7 +101,31 @@ out.each[token].rules = rules;

}
/**
* Nested arrays
*/
if (isArray) {
/**
* The code after the error works fine. However, in order to support
* 2d arrays, we need to implement them inside the declarative
* schema and compiler as well.
*
* For now, it's okay to skip this feature and later work on it
* across all the modules.
*/
throw new Error('2d arrays are currently not supported');
// const item = setArray(
// (out as SchemaNodeArray).each[token].children,
// token,
// isIndexedArray ? tokens[index] : '*',
// )
// return parseFieldForRules(tokens, rules, item, index)
}
/**
* Otherwise continue recursion
*/
return parseFieldForRules(tokens, rules, out.each[token].children, index);
}
/**
* Last item in the list of tokens. we must
* patch the rules here.
*/
if (isLast) {

@@ -62,2 +135,5 @@ setLiteral(out, token, rules);

}
/**
* Current item as an array
*/
if (isArray) {

@@ -67,5 +143,42 @@ const item = setArray(out, token, isIndexedArray ? tokens[index] : '*');

}
/**
* Falling back to object
*/
const item = setObject(out, token);
return parseFieldForRules(tokens, rules, item.children, index);
}
/**
* Parses the schema object to a tree of parsed schema. The
* output is optimized for executing validation rules.
*
* @example
* ```
* parser({
* 'users.*.username': 'required'
* })
*
* // output
*
* {
* users: {
* type: 'array',
* rules: [],
* each: {
* '*': {
* rules: [],
* children: {
* username: {
* type: 'literal',
* rules: [{
* name: 'required',
* args: []
* }]
* }
* }
* }
* }
* }
* }
* ```
*/
function rulesParser(schema) {

@@ -96,2 +209,24 @@ if (schema.schema) {

exports.rulesParser = rulesParser;
/**
* Parses an object of messages to [[ParsedMessages]] list. The messages list
* is simpler than rules tree, since compiler can use the schema tree to find
* the appropriate messages from the flat list of messages.
*
* @example
* ```
* parser({
* 'users.*.username.required': 'Username is required'
* })
*
* // output
*
* {
* fields: {
* 'users.*.username': {
* required: 'Username is required'
* }
* },
* rules: {},
* }
*/
function messagesParser(schema) {

@@ -104,2 +239,5 @@ return Object

const rule = toCamelCase(tokens.pop());
/**
* If token length is 1, then it is a plain rule vs `field.rule`
*/
if (!tokens.length) {

@@ -106,0 +244,0 @@ result.rules[rule] = message;

3

build/src/Types/array.d.ts
import { ParsedRule, TypedSchema, DictTypeDeclarations, ArrayTypeDeclaration, ObjectTypeDeclaration, OptionalArrayTypeDeclaration } from '../Contracts';
/**
* Create an array type schema node
*/
export declare function array(...rules: ParsedRule[]): {

@@ -3,0 +6,0 @@ members: (<Schema extends DictTypeDeclarations | TypedSchema>(schema: Schema) => ArrayTypeDeclaration<Schema extends TypedSchema ? ObjectTypeDeclaration<Schema> : Schema>);

"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const getArrayType_1 = require("../utils/getArrayType");
/**
* Create an array type schema node
*/
function array(...rules) {

@@ -12,2 +23,5 @@ return {

exports.array = array;
/**
* Create an optional array type schema node
*/
array.optional = (...rules) => {

@@ -14,0 +28,0 @@ return {

import { BooleanTypeDeclaration, OptionalBooleanTypeDeclaration, ParsedRule } from '../Contracts';
/**
* Define a boolean
*/
export declare const boolean: {
(...rules: ParsedRule[]): BooleanTypeDeclaration;
/**
* Define an optional boolean
*/
optional(...rules: ParsedRule[]): OptionalBooleanTypeDeclaration;
};
"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const getLiteralType_1 = require("../utils/getLiteralType");
/**
* Define a boolean
*/
exports.boolean = (...rules) => {
return getLiteralType_1.getLiteralType('boolean', rules || [], false);
};
/**
* Define an optional boolean
*/
exports.boolean.optional = (...rules) => {
return getLiteralType_1.getLiteralType('boolean', rules || [], true);
};

@@ -6,3 +6,4 @@ export { array } from './array';

export { schema } from './schema';
export { schema as new } from './schema';
export { string } from './string';
export { tuple } from './tuple';
"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });

@@ -13,2 +21,4 @@ var array_1 = require("./array");

exports.schema = schema_1.schema;
var schema_2 = require("./schema");
exports.new = schema_2.schema;
var string_1 = require("./string");

@@ -15,0 +25,0 @@ exports.string = string_1.string;

import { NumberTypeDeclaration, OptionalNumberTypeDeclaration, ParsedRule } from '../Contracts';
/**
* Define a number
*/
export declare const number: {
(...rules: ParsedRule[]): NumberTypeDeclaration;
/**
* Define an optional number
*/
optional(...rules: ParsedRule[]): OptionalNumberTypeDeclaration;
};
"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const getLiteralType_1 = require("../utils/getLiteralType");
/**
* Define a number
*/
exports.number = (...rules) => {
return getLiteralType_1.getLiteralType('number', rules || [], false);
};
/**
* Define an optional number
*/
exports.number.optional = (...rules) => {
return getLiteralType_1.getLiteralType('number', rules || [], true);
};
import { TypedSchema, ObjectTypeDeclaration, OptionalObjectTypeDeclaration } from '../Contracts';
/**
* Create an object type schema node
*/
export declare function object(): {

@@ -3,0 +6,0 @@ members: (<Schema extends TypedSchema>(schema: Schema) => ObjectTypeDeclaration<Schema>);

"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const getObjectType_1 = require("../utils/getObjectType");
/**
* Create an object type schema node
*/
function object() {

@@ -12,2 +23,5 @@ return {

exports.object = object;
/**
* Create an optional object type schema node
*/
object.optional = () => {

@@ -14,0 +28,0 @@ return {

import { TypedSchema, ParsedTypedSchema } from '../Contracts';
/**
* Parses a schema and returns it's parsed tree along with a
* superficial object to be used as types.
*/
export declare function schema<Schema extends TypedSchema>(definedSchema: Schema): ParsedTypedSchema<Schema>;
"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Parses a schema and returns it's parsed tree along with a
* superficial object to be used as types.
*/
function schema(definedSchema) {

@@ -4,0 +16,0 @@ return Object.keys(definedSchema).reduce((result, key) => {

import { ParsedRule, StringTypeDeclaration, OptionalStringTypeDeclaration } from '../Contracts';
/**
* Define a string
*/
export declare const string: {
(...rules: ParsedRule[]): StringTypeDeclaration;
/**
* Define an optional string
*/
optional(...rules: ParsedRule[]): OptionalStringTypeDeclaration;
};
"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const getLiteralType_1 = require("../utils/getLiteralType");
/**
* Define a string
*/
exports.string = (...rules) => {
return getLiteralType_1.getLiteralType('string', rules || [], false);
};
/**
* Define an optional string
*/
exports.string.optional = (...rules) => {
return getLiteralType_1.getLiteralType('string', rules || [], true);
};
import { ParsedRule, DictTypeDeclarations, TupleTypeDeclaration, OptionalTupleTypeDeclaration } from '../Contracts';
/**
* Create an array with fixed number of children
*/
export declare function tuple(...rules: ParsedRule[]): {

@@ -3,0 +6,0 @@ members: (<Schema extends DictTypeDeclarations[]>(...schema: Schema) => TupleTypeDeclaration<Schema>);

"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const getTupleType_1 = require("../utils/getTupleType");
/**
* Create an array with fixed number of children
*/
function tuple(...rules) {

@@ -12,2 +23,5 @@ return {

exports.tuple = tuple;
/**
* Create an optional array with fixed number of children
*/
tuple.optional = (...rules) => {

@@ -14,0 +28,0 @@ return {

import { TypedSchema, SchemaNodeArray, DictTypeDeclarations, ParsedRule } from '../Contracts';
/**
* Returns runtime node for an array type.
*/
export declare function getArrayType(schema: DictTypeDeclarations | TypedSchema, isOptional: boolean, rules: ParsedRule[]): {
getTree(): SchemaNodeArray;
};
"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const getBaseRules_1 = require("./getBaseRules");
const getObjectType_1 = require("./getObjectType");
/**
* Returns runtime node for an array type.
*/
function getArrayType(schema, isOptional, rules) {

@@ -19,2 +30,6 @@ return {

'*': {
/**
* Nodes of array cannot be marked as required and hence `size`
* rule must be used on array for same
*/
rules: children.rules.filter(({ name }) => name !== 'required'),

@@ -21,0 +36,0 @@ children: children.type === 'object' ? children.children : {},

"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });

@@ -3,0 +11,0 @@ function getBaseRules(dataType, isOptional) {

"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });

@@ -3,0 +11,0 @@ const getBaseRules_1 = require("./getBaseRules");

import { TypedSchema, SchemaNodeObject } from '../Contracts';
/**
* Returns runtime node for an object type.
*/
export declare function getObjectType(schema: TypedSchema, isOptional: boolean): {
getTree(): SchemaNodeObject;
};
"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const getBaseRules_1 = require("./getBaseRules");
/**
* Returns runtime node for an object type.
*/
function getObjectType(schema, isOptional) {

@@ -5,0 +16,0 @@ return {

import { SchemaNodeArray, DictTypeDeclarations, ParsedRule } from '../Contracts';
/**
* Returns runtime node for an array type.
*/
export declare function getTupleType(schemas: DictTypeDeclarations[], isOptional: boolean, rules: ParsedRule[]): {
getTree(): SchemaNodeArray;
};
"use strict";
/*
* indicative-parser
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const getBaseRules_1 = require("./getBaseRules");
/**
* Returns runtime node for an array type.
*/
function getTupleType(schemas, isOptional, rules) {

@@ -10,3 +21,3 @@ return {

rules: getBaseRules_1.getBaseRules('array', isOptional)
.concat([{ name: 'size', args: [schemas.length] }])
.concat([{ name: 'size', args: [schemas.length] }]) // Enforce length
.concat(rules),

@@ -21,2 +32,6 @@ each: {},

payload.each[index] = {
/**
* Nodes of array cannot be marked as required and hence `size`
* rule must be used on array for same
*/
rules: children.rules.filter(({ name }) => name !== 'required'),

@@ -23,0 +38,0 @@ children: children.type === 'object' ? children.children : {},

16

package.json
{
"name": "indicative-parser",
"version": "7.1.2",
"version": "7.1.3",
"description": "Schema parser for Indicative",

@@ -39,4 +39,4 @@ "main": "build/index.js",

"devDependencies": {
"@adonisjs/mrm-preset": "^2.2.2",
"@types/node": "^12.12.14",
"@adonisjs/mrm-preset": "^2.2.3",
"@types/node": "^13.1.0",
"commitizen": "^4.0.3",

@@ -46,12 +46,12 @@ "cz-conventional-changelog": "^3.0.2",

"doctoc": "^1.4.0",
"eslint": "^6.7.2",
"eslint-plugin-adonis": "^1.0.2",
"eslint": "^6.8.0",
"eslint-plugin-adonis": "^1.0.4",
"husky": "^3.1.0",
"japa": "^3.0.1",
"mrm": "^2.0.0",
"mrm": "^2.0.2",
"np": "^5.2.1",
"ts-node": "^8.5.4",
"typedoc": "^0.15.3",
"typedoc": "^0.15.5",
"typedoc-plugin-external-module-name": "^2.1.0",
"typescript": "^3.7.3"
"typescript": "^3.7.4"
},

@@ -58,0 +58,0 @@ "nyc": {

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