0.3.8 • Published 6 years ago

allowed-fields v0.3.8

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

allowed-fields

Description

This module lets developer define white listed and black listed database fields and provides a function to check whether given field is allowed.

Synopsis

TypeScript

import AllowedFields, { Fields } from "allowed-fields";

JavaScript

const AllowedFields = require("allowed-fields");
const fields = new AllowedFields({
  whiteList: { "": "color", member: "*", company: "*", manager: ["name"] },
  blackList: { member: ["salary"] },
});

// Field may be provided with single string as ('table.field').
fields.isAllowed("color"); // true  (color is allowed without relation name)
fields.isAllowed("member.name"); // true  (All fields (*) of member except 'salary' is allowed)
fields.isAllowed("manager.name"); // true  (It is in white list)
fields.isAllowed("member.salary"); // false (It is in black list)
fields.isAllowed("zoo.name"); // false (It is not in white list)
fields.isAllowed("member.*"); // false (Member salary is black listed. All fields (*) except salary are allowed)
fields.isAllowed("company.*"); // true  (All fields (*) of company is in white list)

// Field may be provided with two parameters as ('field', 'table')
fields.isAllowed("name", "member"); // true;
fields.isAllowed("salary", "member"); // false;

Details

This module is a utility for checking whether given fields are allowed according to simple blacklist and whitelist rules.

Blacklist and whitelist are provided using object. Keys are relation (table) names, values are field names. To allow every field in a table *

API

Classes

Typedefs

Interfaces

AllowedFieldsConfig

Kind: global interface
Properties

NameTypeDescription
whiteListFieldsList of allowed identifiers (entities and fields) to be used in query.
blackListFieldsList of identifiers which are prohibited to use in query.

AllowedFields

Kind: global class

new AllowedFields(config)

ParamTypeDescription
configObjectConfiguration
config.whiteListFieldsList of allowed identifiers (entities and fields) to be used in query.
config.blackListFieldsList of identifiers which are prohibited to use in query.

allowedFields.isAllowed(fieldName, relationName) ⇒ boolean

Kind: instance method of AllowedFields
Returns: boolean -

ParamTypeDefaultDescription
fieldNamestringField name to test. i.e 'name'. Also it may contain field name such as 'member.name'
relationNamestring"''"Relation name which field belongs to.

Example

allowedFields.isAllowed("member.name"); // Table and field as a single string.
allowedFields.isAllowed("name", "member"); // Field, Table.

Fields : Object.<string, (string|Array.<string>)>

Kind: global typedef
Example

const fields = {
  "": "name", // Field name without table.
  person: "name", // Single field from `person` table.
  cart: ["name", "color"], // Some fields from `cart` table.
  report: "*", // All fields from `report` table.
};
0.3.8

6 years ago

0.3.7

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago