nuvira
Version:
Nuvira Database. New Database format (Readable & Easy to use), (Inbuilt Schema & constraints & rules & relations).
167 lines • 5.77 kB
TypeScript
export interface ValidateDataParams {
schema?: {
[key: string]: Schema;
};
validations?: any;
data: any;
}
export interface ValidateDataResult {
schemaValidation?: ValidationResult;
dataValidation?: ValidationResult;
}
export interface ValidationResult {
valid: boolean;
errors: string[];
}
/**
* Interface representing a schema definition.
*
* - `type`: An array of allowed type names.
* - `properties`: For an object, defines a schema for each property.
* - `items`: For an array, defines the schema for each element. It can either be a proper Schema
* (with its own `type` field) or a dictionary of Schema objects (which will be normalized).
*/
interface Schema {
type: string[];
properties?: {
[key: string]: Schema;
};
items?: Schema | {
[key: string]: Schema;
};
}
/**
* A class that validates a record against a provided schema dictionary.
*
* Allowed types include:
* - Primitives: "Number", "String", "Boolean", "Date", "Binary", "Uint8Array"
* - Objects: "Object"
* - Arrays:
* * Generic arrays: "Any[]", "AnyArray", "Array", "[]"
* * Specific arrays: "StringArray", "String[]", "NumberArray", "Number[]", "ObjectArray", "Object[]"
* - Special values: "Null", "undefined", "Any"
*/
export declare class SchemaValidator {
private schemaDict;
constructor(schemaDict: {
[key: string]: Schema;
});
/**
* Validates a record against the top-level schema dictionary.
*
* For example, if the schema is:
* {
* Users: {
* type: [ "ObjectArray" ],
* items: { ... }
* }
* }
*
* then this method checks that:
* - The record has a key "Users".
* - The value at "Users" matches the "ObjectArray" schema.
*
* @param record - The record to validate.
* @returns A ValidationResult with `valid` and error details.
*/
validate(record: Record<string, unknown>): ValidationResult;
/**
* Validates a record (object) against a top-level schema dictionary.
*
* Also checks for extra keys not mentioned in the schema.
*/
private validateRecordAgainstSchemaDict;
/**
* Validates a value against a given schema.
*
* If multiple allowed types are specified, the value is valid if it matches at least one.
* Otherwise, detailed error messages are returned.
*
* @param schema - The schema definition.
* @param record - The value to validate.
* @param path - The path of the value (for error messages).
* @returns An array of error messages; an empty array means valid.
*/
private validateAgainstSchema;
/**
* Normalizes the `items` field if it is provided as a dictionary (i.e., without a "type" field).
*
* For example, if you receive:
* items: {
* name: { type: ["String"] },
* age: { type: ["Number"] },
* ...
* }
* then this function wraps it as:
* {
* type: ["Object"],
* properties: { ... }
* }
*
* @param schema - The schema object which may have an `items` field.
* @returns A normalized Schema for the items, or `undefined` if no items are provided.
*/
private normalizeItems;
/**
* Checks if the record matches a single allowed type within the schema.
*
* @param allowedType - The type string to validate against.
* @param schema - The full schema (which may contain additional info like properties or items).
* @param record - The value to validate.
* @param path - The path of the value (for error messages).
* @returns An array of error messages; an empty array means the value is valid for this allowed type.
*/
private checkAgainstAllowedType;
}
/**
* A class that validates record data against a nested validations object.
*
* The validations object is expected to be structured similarly to:
*
* {
* Users: {
* rules: { required: true, maxLength: 50, isUnique: true },
* name: { rules: { required: true, minLength: 3, uppercase: true, isText: true } },
* friends: { rules: { minLength: 3, uppercase: true, isText: true } }
* },
* name: {
* rules: { required: true, minLength: 3, isText: true }
* }
* }
*/
export declare class ValidationRulesValidator {
private validationKeywords;
/**
* Determines if a rule should be applied element-wise if the field value is an array.
*/
private ruleShouldApplyElementWise;
/**
* Validates a record against a nested validations object.
*
* @param record - The record to validate.
* @param validations - The validations object.
* @returns A ValidationResult containing a valid flag and error messages.
*/
validate(record: any, validations: any): ValidationResult;
/**
* Recursively validates a record against the validations object.
*
* @param record - The current value from the record.
* @param validations - The validations object for the current field.
* @param path - The current field path (for error messages).
* @returns An array of error messages.
*/
private validateRecursively;
/**
* Validates a single rule against a field value.
*
* @param ruleName - The name of the rule (e.g., "required", "minLength", "isText", "isNumber").
* @param ruleValue - The value of the rule (e.g., true, 50, 3).
* @param fieldValue - The value of the field from the record.
* @param path - The path to the field (for error messages).
* @returns An array of error messages for this rule.
*/
private validateRule;
}
export {};
//# sourceMappingURL=validator.d.ts.map