nox-validation
v1.7.7
Published
validate dynamic schema
Downloads
1,728
Readme
Nox Validation
nox-validation is a powerful and flexible validation library for both client-side and server-side use. It supports nested fields, alias validation, and customizable rules, making it perfect for complex form data validation.
Installation
npm install nox-validationLibrary Exports
Validation Functions:
validate- The core function for validating data.
Constants:
CONSTANTS- A collection of predefined constants (likely including data types, error messages, validation rules, etc.).
Helper Functions (HELPERS):
validateSingleField- Validates a single field against defined rules.validateType- Performs type checks for data validation.fieldsMapping- Groups fields byschemaIdfor structured access.convertTree- Converts a flat structure into a nested tree format.getValueByDynamicKey- Retrieves a value dynamically from an object using a key path.setValueByDynamicKey- Sets a value dynamically in an object using a key path.
Usage
const { validate, helpers } = require("nox-validation");
const all_fields = [];
const relations = [];
const schema = [
{
_id: "67d2996955b853593b84a0c8",
schema_id: "67d2993655b853593b84a086",
field: "email",
field_type: "Single",
type: "String",
path: "email",
meta: {
field: "email",
interface: "input",
hidden: false,
sort: null,
required: true,
nullable: false,
validations: {
rules: [
{
rule: "matchesRegExp",
matchesRegExp: {
value: "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$/",
},
},
],
},
readonly: false,
},
schema_definition: {
name: "email",
type: "String",
default: null,
unique: true,
},
},
{
_id: "67d2995955b853593b84a0bc",
schema_id: "67d2993655b853593b84a086",
field: "user_name",
field_type: "Single",
type: "String",
path: "user_name",
meta: {
field: "user_name",
interface: "input",
hidden: false,
sort: null,
required: true,
nullable: false,
readonly: false,
},
schema_definition: {
name: "user_name",
type: "String",
default: null,
},
},
];When isSeparatedFields is true
const result = validate({
formData: {
user_name: "John Doe",
email: "[email protected]",
},
isSeparatedFields: true,
fields: schema,
relationalFields: helpers.fieldsMapping(all_fields),
relations: relations,
formId: "67d2993655b853593b84a086",
abortEarly: false,
byPassKeys: [],
apiVersion: "v1",
language: "nl",
maxLevel: 3,
onlyFormFields: false,
language_codes:[]
});When isSeparatedFields is false
const result = validate({
formData: {
user_name: "John Doe",
email: "[email protected]",
},
isSeparatedFields: false,
fields: all_fields,
relationalFields: {},
relations: relations,
formId: "67d2993655b853593b84a086",
abortEarly: false,
byPassKeys: [],
apiVersion: "v1",
language: "nl",
maxLevel: 3,
onlyFormFields: false,
language_codes:[]
});Output Example:
{
status: true,
error: {},
data: {}
}Parameters
Schema Parameter Descriptions:
formData (Object)
- Represents the primary data structure for form submission.
- Contains key-value pairs corresponding to form fields.
formId (ObjectId)
- A unique identifier for the form schema.
- Required when
isSeparatedFieldsistrue, indicating that only form-specific fields are provided.
isSeparatedFields (Boolean)
- Determines how fields are structured in the request.
- If
true→ Only form schema fields are included infields. - If
false→ All fields for a specific tenant are included. Additionally,relationalFieldswill contain{ schema_id: [related fields] }.
relations (Array of Objects)
- Defines the relationships between different entities.
- Contains objects specifying relation types and linked entities.
fields (Array of Objects)
- Holds the form schema fields when
isSeparatedFieldsistrue. - If
false, it contains all fields for the specific tenant.
relationalFields (Object)
- Maps schema IDs to their related fields.
- Used when
isSeparatedFieldsisfalseto maintain relationships between schemas and related fields.
abortEarly (Boolean)
- Determines validation behavior.
- If
true→ Validation stops on the first error encountered. - If
false→ All validation errors are collected and returned.
byPassKeys (Array of Strings)
- Contains field names that should be skipped during validation.
- Allows exclusion of specific fields from validation checks.
apiVersion (String)
- Specifies the API version (
"v1"or"v2"). - Helps in maintaining compatibility and version control.
language (String)
- Specifies the locale language (
"nl"or"en"). - Determines the locale for validation messages.
maxLevel (Number)
- Defines the level for generate tree structure.
onlyFormFields (Boolean)
Indicates the context in which the form is being used.
false: Used when creating new data (full form).true: Used when editing or displaying only existing form fields.
language_codes (Array of IDs)
- Represents the selected translation language IDs.
- Each item in the array should be a valid language code or
_idused for handling multilingual content.
Result
The validate function returns an object with two keys:
status(boolean):trueif validation passed,falseif there were errors.error(object): Contains validation errors, with field paths as keys and error messages as values.
