{Edictor}
is a Javascript essential tool to harness the power of data.
It ensures data reliability, maintains quality,
facilitates regulatory compliance, prevents errors. It can unlock the true value
of data and gain a significant advantage in today's interconnected world.
No overwhelming APIs, the APIs are simple, easy to use. It's designed to enhance javascript expressions and statements to validate any kind of javascript object/data.
declare class DefineField {
constructor(option = {required: false, grant: [], initial: undefined});
instance(...types); // Validate data instance to be one of provided types.
regexp(regexp_); // Validate data with regular expression.
assert(func, msg); // Validate with function to be true or false.
apply(func); // Validate data by using function and set value to returned data.
arrayOf(...validators); // Validate array data.
model(model_class); // Validate nested data.
}
{Edictor}
support building schema for both nested and array data.
Moreover, data pass through schema can becomes atomic instance
which can validate itself in real-time when data has been changed,
garantee that data is always valid.
Example usage:
class Website extends Model {};
Website.define({
title: defineField()
.instance("string")
.assert((title) => { title.length <= 250 },
"Title must have 250 characters or less"),
url: defineField()
.instance("string")
.apply((value) => {new URL(value)})
})
/** Validate data, return validation result object */
const result = Website.test({url: 'https://example.com'});
/** Create atomic data instance */
const website = new Website();
website['url'] = 'https://example.com'; // valid.
website['url'] = 'abc'; // => Throw errors.
Array data validations can be done easily with ArrayOf()
which provides
APIs to validate data by instance type or validation function.
Validations also support higher order array.
array1 = new ArrayOf('string', 'number');
// Higher order array.
array2 = new ArrayOf(['string', 'number'], ['string', 'boolean']);
// These are valid arrays according to the schemas.
array1.push(1, 2, 3, 'a', 'b', 'c');
array2.push(['a',1], ['a',2], [true, false, 'abc']);
{Edictor} is planned to have better APIs for matrix validations in the near future.
When data validations fail, {Edictor}
throws errors and and provide
useful information based on the structure of tesing data.
interface ModelTestResult {
valid?: object, // stores valid data object
invalid?: object, // stores invalid data object
error?: object, // stores error information based on data structure.
errorMessage?: '' // error message
}
You can try {Edictor}
in your browser console. Model, ArrayOf, defineField
has been imported and ready to use. For example:
class SomeModel extends Model {};
SomeModel.define({}); // define your model.