0.7.0 • Published 11 months ago

tychecker v0.7.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

tychecker - a type checker

⚠️ tycheck is still in development, originally created for shortlnk project, it evolve for the moment at the same time as shortlnk. But its development is to think for a general use and integrable by other projects.

Features

  • String Validator Function
  • Object Validator Function
    • Object Entry Validator Function
    • Object Entry Validator for multiple keys
  • Number Validator Function
    • Number Validator Function support Big Int
  • Date Validator Function
  • Array Validator Function
    • Array Entry Validator Function
  • - Pipeline System
  • Create configuration with OpenAPI file

Installation guide

npm install tychecker

Documentation

global uses

import on CommonJS

const { stringValidator } = require("tychecker");

import on EcmaScript

import { stringValidator } from "tychecker";

simple example

const str = "Hello World";
const { length } = str;

const validator = stringValidator({
  equLength: length,
});

console.log(validator(str)); // true

References

Functions

stringValidator(config)

Parameter config StringValidatorConfig | Properties | Type | Required | Description | |------------|--------|----------|----------------| | equLength | Number | | exact string length | | minLength | Number | | minimum string length | | maxLength | Number | | maximum string length | | regex | Regex | | Applying regex test |

Return StringValidatorFn

const str = "Hello World!";
const validator = stringValidator({ equLength: str.length, regex: /Hello/ });
validator(str);

objectValidator(config)

Parameters config ObjectValidatorConfig | Properties | Type | Required | Description | |------------------|----------------------------------------------------------------------------------------------------------------|----------|----------------------------------------| | allowEmptyObject | Boolean | | allow empty object | | minLength | Number | | minimum keys count | | maxLength | Number | | maximum keys count | | equLength | Number | | exact keys count | | entries | EntryObjectInstance[] | EntryObjectValidatorConfig[] | | Target one key of object for testing data type |

Return ObjectValidatorFn

const obj = { name: "bob", age: 24 };
const validator = objectValidator({
  allowEmptyObject: true,
  equKeys: Object.keys(obj).length,
  entries: [entryObjectValidator({
    key: "name",
    required: true,
  })],
});
const result = validator(obj);

entryObjectValidator(config)

Parameter config EntryObjectValidatorConfig | Properties | Type | Required | Description | |------------|------------------------------------------------------|----------|---------------------------------------------------------------------| | key | ObjectKeyType | ObjectKeyType[] | symbol | yes | Target one key of object | | Required | Boolean | | If key is required on object | | dataType | DataType | DataType[] | | Check target value type | | validator | ValidatorFn | | Apply Validator function | | validators | ValidatorFn[] | | Apply each Validator function, process pass when once Validator return true |

Return EntryObjectInstance

const obj = { name: "bob", age: 24 };
const entryObjectValidatorFn = entryObjectValidator({
  key: "name",
  required: true,
  dataType: "string",
});
const validator = objectValidator({
  entries: [entryObjectValidatorFn],
});
const result = validator(obj);

dataTypeChecker(data, types)

Parameter data any

Parameter types DataType | DataType[]

Return Boolean

const str = "Hello World!";
dataTypeChecker(str, "string");

numberValidatorFunction(config)

Parameter config NumberValidatorConfig | Properties | Type | Required | Description | |--------------------|----------------------|----------|------------------------------------------------------------------| | equAt | Number | BigInt | | Equal at entry number|bigint | | minAt | Number | BigInt | | Minimum at entry number|bigint | | maxAt | Number | BigInt | | Maximum at entry number|bigint | | transformStringTo | 'number' | 'bigint' | | Transform entry string to number or bigint before apply validation tests | | allowBigInt | Boolean | | Allow BigInt type on entry | | mustBeBigInt | Boolean | | Entry must be a BigInt                            | | allowFloat | Boolean | | Allow Float on entry | | allowInfinite | Boolean | | Allow Infinite on entry | | allowNoSafeInteger | Boolean | | Allow No Safe Integer on entry | | allowNegatifAmout | Boolean | | Allow Negatif Amount on entry |

Return NumberValidatorFn

const num = 5;
const validator = numberValidator({ equAt: num });
validator(str);

dateValidator(config)

Parameter config DateValidatorConfig | Properties | Type | Required | Description | |--------------------|----------------------|----------|-----------------| | transformDate | Boolean | | transform entry to Date | | equAt | Date | Number | | Equal to date | | minAt | Date | Number | | Minimum to date | | maxAt | Date | Number | | Maximum to date |

Return DateValidatorFn

const date = new Date('2023-06-04T02:23:08.719Z');
const validator = dateValidator({ equAt: date });
validator(str);

Objects

regex

PropertyType
emailTegex
const { stringValidator, regex } = require("tycheck");

stringValidator({
  regex: regex.email,
});

Types

DataType

Type String - string | number | array | object | bigint| function| symbol| undefined | date| null| boolean | regex

StringValidatorFn

Type Function - (str: string) => boolean

ObjectValidatorFn

Type Function - (obj: any) => boolean

EntryObjectInstanceValidatorFn

Type Function - (obj: any) => boolean

NumberValidatorFn

Type Function - (num: string | number | bigint) => boolean

DateValidatorFn

Type Function - (date: Date | number | string) => boolean

ValidatorFn

Type Function - StringValidatorFn | ObjectValidatorConfig | NumberValidatorFnDateValidatorFn

ObjectKeyType

Type Object - string | number | symbol

ObjectValidatorConfig

Type Object | properties | Type | Required | Description | |------------------|----------------------------------------------------------------------------------------------------------------|----------|----------------------------------------| | allowEmptyObject | Boolean | | allow empty object | | minLength | Number | | minimum keys count | | maxLength | Number | | maximum keys count | | equLength | Number | | exact keys count | | entries | EntryObjectInstance[] | EntryObjectValidatorConfig | | Target one key of object for testing data type |

EntryObjectValidatorConfig

Type Object | Property | Type | Required | Description | |------------|------------------------------------------------------|----------|---------------------------------------------------------------------| | key | ObjectKeyType | ObjectKeyType[] | regex | yes | Target one key of object | | Required | Boolean | | If key is required on object | | dataType | DataType | DataType[] | | Check target value type | | validator | ValidatorFn | | Apply Validator function | | validators | ValidatorFn[] | | Apply each Validator function, process pass when once Validator return true |

EntryObjectInstance

type Object | Property | Type | Required | readonly | Description | |-------------|----------|----------|----------|---------------------------------------------------| | _tyInstance | Boolean | yes | yes | Object created by entryObjectValidator function | | validator | Function | yes | yes | Apply Validator function on target value |

StringValidatorConfig

type Object

PropertyTypeRequireddescriptio
equLengthNumberEqual string length
minLengthNumberMinimum string length
maxLengthNumberMaximum string length
regexRegexApply Regex test method on string

NumberValidatorConfig

type Object

PropertiesTypeRequiredDescription
equAtNumber | BigIntEqual at entry number|bigint
minAtNumber | BigIntMinimum at entry number|bigint
maxAtNumber | BigIntMaximum at entry number|bigint
transformStringTo'number' | 'bigint'Transform entry string to number or bigint before apply validation tests
allowBigIntBooleanAllow BigInt type on entry
mustBeBigIntBooleanEntry must be a BigInt                            
allowFloatBooleanAllow Float on entry
allowInfiniteBooleanAllow Infinite on entry
allowNoSafeIntegerBooleanAllow No Safe Integer on entry
allowNegatifAmoutBooleanAllow Negatif Amount on entry

DateValidatorConfig

type Object

PropertiesTypeRequiredDescription
transformDateBooleantransform entry to Date
equAtDate | NumberEqual to date
minAtDate | NumberMinimum to date
maxAtDate | NumberMaximum to date

Example

Multiple type Object validator

const { objectValidatorn entryObjectValidator } = require('tychecker');

const obj = (i) => ({
    foo: ['bar', 1, {}][i]
});

const validator = objectValidator({
    entries: [
        entryObjectValidator({
            key: 'foo',
            dataType: ['string', 'number']
        })
    ]
})

validator(obj(0)); // true
validator(obj(1)); // true
validator(obj(2)); // false

Deep data type Validator

const { objectValidator, entryObjectValidator, stringValidator } = require("tychecker");

const obj = {
  foo: "bar",
};

const validator = objectValidator({
  entries: [
    entryObjectValidator({
      key: "foo",
      dataType: "string",
      validator: stringValidator({
        minLength: 2,
      }),
    }),
  ],
});

validator(obj); // true

Deep Object Validator

const { objectValidator, entryObjectValidator } = require("tychecker");

const deepObject = {
  result: {
    foo: "bar",
  },
};

const validator = objectValidator({
  entries: [
    entryObjectValidator({
      key: "result",
      validator: objectValidator({
        entries: [
          entryObjectValidator({
            key: "foo",
            equKeys: 1,
          }),
        ],
      }),
    }),
  ],
});

validator(deepObject); // true

Use Object instead of entryObjectValidator function

const { objectValidator } = require("tychecker");

const obj = {
  foo: "bar",
};

const validator = objectValidator({
  entries: [{
    key: "foo",
    equKeys: 1,
  }],
});

validator(obj)

Multiple keys selector on entry validator function

const { entryObjectValidator } = require("tychecker");

const obj = {
  hello: 'world',
  foo: "bar",
};

const validator = entryObjectValidator({
  entries: [
    entryObjectValidator({
      key: ['hello', 'foo'],
      dataType: 'string'
    }),
  ]
});

validator(obj);

Credit

© 2023 - MIT

0.3.0

12 months ago

0.6.2

11 months ago

0.5.0

11 months ago

0.3.2

12 months ago

0.4.0

11 months ago

0.3.1

12 months ago

0.7.0

11 months ago

0.6.1

11 months ago

0.6.0

11 months ago

0.2.3

12 months ago

0.2.2

12 months ago

0.2.1

12 months ago

0.2.0

12 months ago

0.1.0

12 months ago