Skip to content

Commit

Permalink
Merge pull request #19 from medishen/dev/v2
Browse files Browse the repository at this point in the history
Refactor Validation System and Enhance Testing for Improved Flexibility and Reliability
  • Loading branch information
0xii00 authored Jan 21, 2025
2 parents ee46642 + 30373db commit 91f47e5
Show file tree
Hide file tree
Showing 30 changed files with 632 additions and 194 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ jobs:
node-version: 20
- name: Install dependencies
run: npm install
- name: Build Dependencies
run: npm run build
- name: Run unit tests
run: npm run test:unit
9 changes: 2 additions & 7 deletions lib/common/interfaces/validator.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ValidationSchema } from '../types';
import { RuleString, RuleType, ValidationOperator } from '../types/validator.type';
import { RuleString, ValidationMessages, ValidationOperator } from '../types/validator.type';
import { Constructor } from './module.interface';
export interface ValidationField {
/**
Expand All @@ -10,7 +10,7 @@ export interface ValidationField {
/**
* Custom error messages for specific rule types.
*/
messages?: Partial<Record<RuleType, string>>;
messages?: ValidationMessages;
/**
* Additional options for the validation field.
*/
Expand Down Expand Up @@ -53,11 +53,6 @@ export interface ValidationOptions<T> {
* The schema class to validate if the condition is met.
*/
schema: Constructor<any>;

/**
* Custom error message to show if the condition is not met.
*/
message: string;
}[];
}
export interface SchemaOptions {
Expand Down
4 changes: 4 additions & 0 deletions lib/common/types/validator.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ export type ValidationOperator =
| 'notMatches'; // Value must not match a specific regex
export type NestedSchemas<T> = Record<string, { schemaClass: Constructor<T>; options: ValidationOptions<T> }>;
export type ValidationSchema = 'body' | 'query' | 'headers' | 'params';
/** Validation messages for each rule type */
export type ValidationMessages = Partial<Record<RuleType | 'custom' | 'dependsOn', string>> & {
dependsOnRules?: Record<ValidationOperator, string>;
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Reflector from '../metadata';
import { ValidationMetadataKey } from '../common/enums';
import { SchemaOptions, ValidationField, ValidationOptions } from '../common/interfaces';
import { ValidationMessages } from '../validator/config';
import { isClass } from '../utils';
import { ValidationMessages } from '../common/types';
export function Rule(options?: { messages?: ValidationMessages; options?: ValidationField['options'] }): PropertyDecorator {
return function (target, propertyKey) {
const instance = new (target.constructor as any)();
Expand All @@ -25,7 +26,7 @@ export function NestedSchema<T = any>(options?: ValidationOptions<T>): PropertyD
return function (target, propertyKey) {
const instance = new (target.constructor as any)();
let schemaClass: any = instance[propertyKey].constructor;
if (schemaClass.toString().includes('[native code]')) {
if (isClass(schemaClass)) {
schemaClass = instance[propertyKey];
}
const nestedSchemas = Reflector.get(ValidationMetadataKey.NESTED, target.constructor) ?? {};
Expand Down
File renamed without changes.
16 changes: 8 additions & 8 deletions lib/decorator/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export * from './Cache';
export * from './Controller';
export * from './Guards';
export * from './MultiLang';
export * from './Transform';
export * from './http';
export * from './Cache.decorator';
export * from './Controller.decorator';
export * from './Guards.decorator';
export * from './MultiLang.decorator';
export * from './Transform.decorator';
export * from './http.decorator';
export * from './module';
export * from './Middleware';
export * from './Validator';
export * from './Middleware.decorator';
export * from './Validator.decorator';
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/decorator/module/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './Injector';
export * from './Module';
export * from './Module.decorator';
3 changes: 2 additions & 1 deletion lib/validator/RuleAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ export class RuleAction {
static filter<T>(schemaClass: Constructor<T>, pick?: (keyof T)[], omit?: (keyof T)[]): Record<string, ValidationField> {
const rules: Record<string, ValidationField> = Reflector.get(ValidationMetadataKey.RULES, schemaClass) ?? {};
const filteredRules: Record<string, ValidationField> = {};

if (pick) {
for (const key of pick) {
if (rules[key as string]) {
if (key in rules) {
filteredRules[key as string] = rules[key as string];
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/validator/ValidationError.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export class ValidationError {
errors: Record<string, Record<string, string[]>> = {};

addError(section: string, field: string, message: string[]): void {
if (!this.errors[section]) this.errors[section] = {};
if (!this.errors[section][field]) this.errors[section][field] = [];
this.errors[section][field].push(...message);
}
/** Check if there are any errors */
hasErrors(): boolean {
return Object.keys(this.errors).length > 0;
}
}
90 changes: 64 additions & 26 deletions lib/validator/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,59 @@ export class ValidatorProcess {
/** Apply validation to the provided schema and data */
static async validate(schemaClass: Constructor<any>, data: Record<string, any>, returnFirstError: boolean = false) {
const validationErrors = new ValidationError();

const nestedSchemas: NestedSchemas<any> = Reflector.get(ValidationMetadataKey.NESTED, schemaClass) ?? {};

for (const [fieldName, nestedSchema] of Object.entries(nestedSchemas)) {
const validatedSchemas = await this.validateConditions(nestedSchemas, data, validationErrors, returnFirstError);

await this.validateFieldsForSchemas(nestedSchemas, validatedSchemas, data, validationErrors, returnFirstError);

return validationErrors.errors;
}
/** Validate all conditions defined in schema options */
private static async validateConditions(nestedSchemas: NestedSchemas<any>, data: Record<string, any>, validationErrors: ValidationError, returnFirstError: boolean): Promise<Set<Constructor<any>>> {
const validatedSchemas = new Set<Constructor<any>>();
const conditions = Object.values(nestedSchemas).filter((schema) => schema.options.conditions);

for (const nestedSchema of conditions) {
const { schemaClass: nestedSchemaClass, options: schemaOptions } = nestedSchema;
ValidatorProcess.applySchemaOptions(nestedSchemaClass, schemaOptions, nestedSchemas);
await ValidatorProcess.validateFields(nestedSchemaClass, Reflector.get(ValidationMetadataKey.RULES, nestedSchemaClass), data, validationErrors, returnFirstError);

if (schemaOptions.conditions) {
for (const condition of schemaOptions.conditions) {
const errors = await this.validateCondition(condition, nestedSchemas, data, validationErrors, returnFirstError);
if (errors) {
validatedSchemas.add(nestedSchemaClass);
validatedSchemas.add(condition.schema);
}
}
}
}
return validationErrors.errors;

return validatedSchemas;
}
/** Validate fields for schemas that have not been validated by conditions */
private static async validateFieldsForSchemas(
nestedSchemas: NestedSchemas<any>,
validatedSchemas: Set<Constructor<any>>,
data: Record<string, any>,
validationErrors: ValidationError,
returnFirstError: boolean,
) {
for (const nestedSchema of Object.values(nestedSchemas)) {
const { schemaClass: nestedSchemaClass, options: schemaOptions } = nestedSchema;

if (validatedSchemas.has(nestedSchemaClass)) {
continue; // Skip schemas already validated by conditions
}

// Apply schema options like pick, omit, etc.
this.applySchemaOptions(nestedSchemaClass, schemaOptions);

// Validate the fields of the schema
await this.validateFields(nestedSchemaClass, data, validationErrors, returnFirstError);
}
}
/** Apply schema options like pick, omit, and conditions */
private static applySchemaOptions<T>(schemaClass: Constructor<any>, schemaOptions: ValidationOptions<T>, nestedSchemas: NestedSchemas<T>) {
private static applySchemaOptions<T>(schemaClass: Constructor<any>, schemaOptions: ValidationOptions<T>) {
if (!schemaOptions) return;

if (schemaOptions.pick || schemaOptions.omit) {
Expand All @@ -32,39 +72,37 @@ export class ValidatorProcess {
const filteredRules = RuleAction.filter(schemaClass, schemaOptions.pick, schemaOptions.omit);
Reflector.define(ValidationMetadataKey.RULES, filteredRules, schemaClass);
}

if (schemaOptions.conditions) {
for (const condition of schemaOptions.conditions) {
ValidatorProcess.validateCondition(condition, nestedSchemas);
}
}
}

/** Validate a condition from schema options */
private static validateCondition<T>(condition: { schema: Constructor<any>; message: string }, nestedSchemas: NestedSchemas<T>) {
private static async validateCondition<T>(
condition: { schema: Constructor<any> },
nestedSchemas: NestedSchemas<T>,
data: Record<string, any>,
validationErrors: ValidationError,
returnFirstError: boolean,
) {
const foundSchema = Object.values(nestedSchemas).find((schema) => schema.schemaClass === condition.schema);
if (!foundSchema) {
throw new Error(`Schema '${condition.schema.name}' in conditions is not defined in @NestedSchema.`);
}

const schemaMetadata = Reflector.get(ValidationMetadataKey.SCHEMA, foundSchema.schemaClass);
if (!schemaMetadata) {
throw new Error(`Metadata not found for schema '${condition.schema.name}'.`);
await ValidatorProcess.validateFields(foundSchema.schemaClass, data, validationErrors, returnFirstError);
if (validationErrors.hasErrors()) {
return validationErrors.errors;
}
return null;
}

/** Validate fields and collect errors */
private static async validateFields<T>(
nestedSchemaClass: Constructor<T>,
rules: Record<string, ValidationField>,
data: Record<string, any>,
validationErrors: ValidationError,
returnFirstError: boolean,
) {
private static async validateFields<T>(nestedSchemaClass: Constructor<T>, data: Record<string, any>, validationErrors: ValidationError, returnFirstError: boolean) {
const rules: Record<string, ValidationField> = Reflector.get(ValidationMetadataKey.RULES, nestedSchemaClass);
const schemaMetadata: SchemaOptions = Reflector.get(ValidationMetadataKey.SCHEMA, nestedSchemaClass);

if (!schemaMetadata) {
throw new Error(`Metadata not found for schema '${nestedSchemaClass.name}'.`);
}
const defaultRules: RuleString = schemaMetadata.defaultRules ?? [];
const sectionData = data[schemaMetadata.section!] ?? {};

for (const [fieldKey, fieldRules] of Object.entries(rules)) {
const mergedRules = {
...fieldRules,
Expand All @@ -76,7 +114,7 @@ export class ValidatorProcess {

if (fieldErrors.length) {
validationErrors.addError(schemaMetadata.section!, fieldKey, fieldErrors);
if (returnFirstError) return validationErrors.errors;
if (returnFirstError) return;
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions lib/validator/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { RuleType, ValidationOperator } from '../common/types/';
/** Validation messages for each rule type */
export type ValidationMessages = Partial<Record<RuleType | 'custom' | 'dependsOn', string>> & {
dependsOnRules?: Record<ValidationOperator, string>;
};
import { ValidationMessages } from '../common/types';

/** Default error messages for validation rules */
export const DefaultMessages: ValidationMessages = {
// Core validation messages
Expand Down
97 changes: 1 addition & 96 deletions lib/validator/index.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1 @@
import { NestedSchema, Rule, Schema } from '../decorator';
import { ValidatorProcess } from './Validator';
@Schema({
section: 'headers',
defaultRules: ['required'],
})
export class HeaderSchema {
@Rule()
example_header = ['boolean'];
}

@Schema({
section: 'body',
defaultRules: ['required'],
})
export class BodySchema {
@Rule({
messages: {
required: 'The username is required.',
string: 'The username must be a string.',
min: 'The username must be at least 3 characters long.',
},
})
username = ['string', 'min:3'];

@Rule({
messages: {
required: 'The email is required.',
email: 'The email must be a valid email address.',
},
})
email = ['string', 'email'];

@Rule({
messages: {
required: 'The password is required.',
string: 'The password must be a string.',
min: 'The password must be at least 8 characters long.',
},
})
password = ['min:8'];
@Rule({
messages: {
required: 'The "isActive" field is required.',
boolean: 'The "isActive" field must be a boolean.',
},
})
isActive = ['boolean'];
@Rule({
messages: {
string: 'The "notes" field must be a string.',
},
})
notes? = ['string'];
}

export class UserSchema {
@NestedSchema({
conditions: [
{
message: '',
schema: HeaderSchema,
},
],
})
body = BodySchema;
@NestedSchema()
headers = HeaderSchema;
}
const userSchame = new UserSchema();
// const username = userSchame.body.username; /** I want username errors to be returned when the user says this. What should I do? */
// console.log('username:', username); // like this {"The username is required."}

const example3: {
body: any;
headers: any;
} = {
body: {
username: 'admin',
password: 'Admin123',
email: 'invalid',
isActive: true,
notes: 'Notes about the admin.',
},
headers: {
example_header: 'hello',
},
};

// Validation Process
(async () => {
console.log('=== Validation Errors: Example 3 ===');
const result = await ValidatorProcess.validate(UserSchema, example3);

console.log(JSON.stringify(result, null, 2));
})();
export * from './Validator';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test": "npm run test:unit",
"test:unit": "mocha --require ts-node/register test/unit/**/*.spec.ts",
"benchmark": "ts-node ./benchmark/gland.ts",
"build": "rm -r dist/* & tsc",
"build": "tsc",
"chmod": "chmod +x ./scripts/release.sh",
"release": "npm run chmod && ./scripts/release.sh"
},
Expand Down
Loading

0 comments on commit 91f47e5

Please sign in to comment.