# 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

```bash
npm install nox-validation
```

## Library 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 by `schemaId` for 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

```javascript
const { validate, helpers } = require("nox-validation");

const all_fields = []; // fields of all collection

const relations = []; // all relations data

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`

```javascript
const result = validate({
  formData: {
    user_name: "John Doe",
    email: "john.doe@example.com",
  },
  isSeparatedFields: true,
  fields: schema,
  relationalFields: helpers.fieldsMapping(all_fields),
  relations: relations,
  formId: "67d2993655b853593b84a086",
  abortEarly: false,
  byPassKeys: [],
  apiVersion: "v1",
  language: "nl",
  maxLevel: 3,
});
```

#### When `isSeparatedFields` is `false`

```javascript
const result = validate({
  formData: {
    user_name: "John Doe",
    email: "john.doe@example.com",
  },
  isSeparatedFields: false,
  fields: all_fields,
  relationalFields: {},
  relations: relations,
  formId: "67d2993655b853593b84a086",
  abortEarly: false,
  byPassKeys: [],
  apiVersion: "v1",
  language: "nl",
  maxLevel: 3
});
```

#### Output Example:

```javascript
{
  status: true, // Validation passed
  error: {},    // Empty if no errors
  data: {}      // Validated 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 `isSeparatedFields` is `true`, 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 in `fields`.
- **If `false`** → All fields for a specific tenant are included. Additionally, `relationalFields` will 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 `isSeparatedFields` is `true`.
- If `false`, it contains all fields for the specific tenant.

#### `relationalFields` (Object)

- Maps schema IDs to their related fields.
- Used when `isSeparatedFields` is `false` to 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.

## Result

The `validate` function returns an object with two keys:

- `status` (_boolean_): `true` if validation passed, `false` if there were errors.
- `error` (_object_): Contains validation errors, with field paths as keys and error messages as values.
