⚠️ 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.
- 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
npm install tychecker
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
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);
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);
Parameter config EntryObjectValidatorConfig
Properties | Type | Required | Description |
---|---|---|---|
key | ObjectKeyType | ObjectKeyType[] | symbol |
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);
Parameter data any
Parameter types DataType
| DataType
[]
Return Boolean
const str = "Hello World!";
dataTypeChecker(str, "string");
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);
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);
Property | Type |
---|---|
Tegex |
const { stringValidator, regex } = require("tycheck");
stringValidator({
regex: regex.email,
});
Type [String] - string
| number
| array
| object
| bigint
|
function
| symbol
| undefined
| date
| null
| boolean
| regex
Type [Function] - (str: string) => boolean
Type [Function] - (obj: any) => boolean
Type [Function] - (obj: any) => boolean
Type [Function] - (num: string | number | bigint) => boolean
Type [Function] - (date: Date | number | string) => boolean
Type [Function] - StringValidatorFn
|
ObjectValidatorConfig
| NumberValidatorFn
| DateValidatorFn
Type [Object] - string
| number
| symbol
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 |
Type [Object]
Property | Type | Required | Description |
---|---|---|---|
key | ObjectKeyType | ObjectKeyType[] | regex |
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 |
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 |
type [Object]
Property | Type | Required | descriptio |
---|---|---|---|
equLength | Number | Equal string length | |
minLength | Number | Minimum string length | |
maxLength | Number | Maximum string length | |
regex | Regex | Apply Regex test method on string |
type [Object]
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 |
type [Object]
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 |
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
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
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
const { objectValidator } = require("tychecker");
const obj = {
foo: "bar",
};
const validator = objectValidator({
entries: [{
key: "foo",
equKeys: 1,
}],
});
validator(obj)
const { entryObjectValidator } = require("tychecker");
const obj = {
hello: 'world',
foo: "bar",
};
const validator = entryObjectValidator({
entries: [
entryObjectValidator({
key: ['hello', 'foo'],
dataType: 'string'
}),
]
});
validator(obj);
- Author - shaynlink
© 2023 - MIT