Antity.js is an Open source library for easy entity management.
- Only 1 small dependency to check inputs variables
- Very lightweight
- Thoroughly tested
- Works in Javascript, Typescript
- Can be used as EcmaScrypt module
- Written in Typescript
- node: 22
This is the oldest targeted versions. The library should work properly on older versions of Node.js but we do not support it officially.
$ npm i @dwtechs/antity
import { Entity } from "@dwtechs/antity";
import { normalizeName, normalizeNickname } from "@dwtechs/checkard";
const entity = new Entity("consumers", [
{
key: "id",
type: "integer",
min: 0,
max: 120,
typeCheck: true,
methods: ["GET", "PUT", "DELETE"],
required: true,
safe: true,
sanitize: true,
normalize: true,
validate: true,
sanitizer: null,
normalizer: null,
validator: null,
},
{
key: "firstName",
type: "string",
min: 0,
max: 255,
typeCheck: true,
methods: ["GET", "POST", "PUT", "DELETE"],
required: true,
safe: true,
sanitize: true,
normalize: true,
validate: true,
sanitizer: null,
normalizer: normalizeName,
validator: null,
},
{
key: "lastName",
type: "string",
min: 0,
max: 255,
typeCheck: true,
methods: ["GET", "POST", "PUT", "DELETE"],
required: true,
safe: true,
sanitize: true,
normalize: true,
validate: true,
sanitizer: null,
normalizer: normalizeName,
validator: null,
},
{
key: "nickname",
type: "string",
min: 0,
max: 255,
typeCheck: true,
methods: ["GET", "POST", "PUT", "DELETE"],
required: true,
safe: true,
sanitize: true,
normalize: true,
validate: true,
sanitizer: null,
normalizer: normalizeNickname,
validator: null,
},
]);
// add a consumer. Used when loggin in from user service
router.post("/", entity.normalize, entity.validate, ...);
type Type =
"boolean" |
"string" |
"number" |
"integer" |
"float" |
"even" |
"odd" |
"positive" |
"negative" |
"powerOfTwo" |
"ascii" |
"array" |
"jwt" |
"symbol" |
"email" |
"regex" |
"json" |
"ipAddress" |
"slug" |
"hexadecimal" |
"date" |
"timestamp" |
"function" |
"htmlElement" |
"htmlEventAttribute" |
"node" |
"object";
type Method = "GET" | "PATCH" | "PUT" | "POST" | "DELETE";
class Property {
key: string;
type: Type;
min: number | Date;
max: number | Date;
required: boolean;
safe: boolean;
typeCheck: boolean;
methods: Method[];
sanitize: boolean;
normalize: boolean;
validate: boolean;
sanitizer: Function | null;
normalizer: Function | null;
validator: Function | null;
};
class Entity {
constructor(name: string, properties: Property[]);
get name(): string;
get unsafeProps(): string[];
get properties(): Property[];
set name(name: string);
/**
* Retrieves a property from the `properties` array that matches the specified key.
*
* @param {string} key - The key of the property to retrieve.
* @returns {Property | undefined} - The property object if found, otherwise `undefined`.
*/
getProp(key: string): Property | undefined;
/**
* Normalizes an array of records by applying sanitization and normalization
* rules defined in the `properties` of the class.
*/
normalize(req: Request, _res: Response, next: NextFunction): void;
/**
* Validates a set of rows against the defined properties and operation/method.
*
* If a property is required and missing, or if it fails the control checks, the function returns an error message.
* Otherwise, it returns `null` indicating successful validation.
*/
validate(req: Request, _res: Response, next: NextFunction): void;
}
normalize() and validate() methods are made to be used as Express.js middlewares. Each method will look for data to work on in the req.body.rows parmeter.
Any of these can be passed into the options object for each function.
Name | Type | Description | Default value |
---|---|---|---|
key | string | Name of the property | |
type | Type | Type of the property | |
min | number | Date | Minimum value | 0 | 1900-01-01 |
max | number | Date | Maximum value | 999999999 | 2200-12-31 |
required | boolean | Is this property required on insert | false |
safe | boolean | Is this property safe to send to the client | true |
typeCheck | boolean | Type is checked if true | false |
methods | Method[] | SQL DML operations concerned by the property | [ "select", "insert", "update", "merge", "delete" ] |
sanitize | boolean | Sanitize the property if true | true |
normalize | boolean | Normalize the property if true | false |
validate | boolean | validate the property if true | true |
sanitizer | ((v:any) => any) | null | Sanitizer function if sanitize is true | null |
normalizer | ((v:any) => any) | null | Normalizer function if normalize is true | null |
validator | ((v:any, min:number, max:number, typeCheck:boolean) => any) | null | validator function if validate is true | null |
- Min and max parameters are not used for boolean type
- TypeCheck Parameter is not used for boolean, string and array types
Antity.js is still in development and we would be glad to get all the help you can provide. To contribute please read contributor.md for detailed installation guide.
Purpose | Choice | Motivation |
---|---|---|
repository | Github | hosting for software development version control using Git |
package manager | npm | default node.js package manager |
language | TypeScript | static type checking along with the latest ECMAScript features |
module bundler | Rollup | advanced module bundler for ES6 modules |
unit testing | Jest | delightful testing with a focus on simplicity |