Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation error handling #45

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/api/decisions/decisions.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Controller, Post, Query, Body, HttpException, HttpStatus } from '@nestjs/common';
import { DecisionsService } from './decisions.service';
import { EvaluateDecisionDto, EvaluateDecisionWithContentDto } from './dto/evaluate-decision.dto';
import { ValidationError } from './validations/validation.error';

@Controller('api/decisions')
export class DecisionsController {
Expand All @@ -11,7 +12,11 @@ export class DecisionsController {
try {
return await this.decisionsService.runDecisionByContent(ruleContent, context, { trace });
} catch (error) {
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
if (error instanceof ValidationError) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
} else {
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/api/decisions/decisions.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { ZenEngine, ZenDecision, ZenEvaluateOptions } from '@gorules/zen-engine';
import { DecisionsService } from './decisions.service';
import { ValidationService } from './validations/validations.service';
import { readFileSafely } from '../../utils/readFile';

jest.mock('../../utils/readFile', () => ({
Expand All @@ -11,6 +12,7 @@ jest.mock('../../utils/readFile', () => ({

describe('DecisionsService', () => {
let service: DecisionsService;
let validationService: ValidationService;
let mockEngine: Partial<ZenEngine>;
let mockDecision: Partial<ZenDecision>;

Expand All @@ -23,11 +25,13 @@ describe('DecisionsService', () => {
};

const module: TestingModule = await Test.createTestingModule({
providers: [ConfigService, DecisionsService, { provide: ZenEngine, useValue: mockEngine }],
providers: [ConfigService, DecisionsService, ValidationService, { provide: ZenEngine, useValue: mockEngine }],
}).compile();

service = module.get<DecisionsService>(DecisionsService);
validationService = module.get<ValidationService>(ValidationService);
service.engine = mockEngine as ZenEngine;
jest.spyOn(validationService, 'validateInputs').mockImplementation(() => {});
});

describe('runDecision', () => {
Expand Down
13 changes: 11 additions & 2 deletions src/api/decisions/decisions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ZenEngine, ZenEvaluateOptions } from '@gorules/zen-engine';
import { ConfigService } from '@nestjs/config';
import { RuleContent } from '../ruleMapping/ruleMapping.interface';
import { readFileSafely, FileNotFoundError } from '../../utils/readFile';
import { ValidationService } from './validations/validations.service';
import { ValidationError } from './validations/validation.error';

@Injectable()
export class DecisionsService {
Expand All @@ -16,12 +18,19 @@ export class DecisionsService {
}

async runDecisionByContent(ruleContent: RuleContent, context: object, options: ZenEvaluateOptions) {
const validator = new ValidationService();
const ruleInputs = ruleContent?.nodes?.filter((node) => node.type === 'inputNode')[0]?.content;
try {
validator.validateInputs(ruleInputs, context);
const decision = this.engine.createDecision(ruleContent);
return await decision.evaluate(context, options);
} catch (error) {
console.error(error.message);
throw new Error(`Failed to run decision: ${error.message}`);
if (error instanceof ValidationError) {
throw new ValidationError(`Invalid input: ${error.message}`);
} else {
console.error(error.message);
throw new Error(`Failed to run decision: ${error.message}`);
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/api/decisions/validations/validation.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export class ValidationError extends Error {
constructor(message: string) {
super(message);
this.name = 'ValidationError';
Object.setPrototypeOf(this, ValidationError.prototype);
}

getErrorCode(): string {
return 'VALIDATION_ERROR';
}

toJSON(): object {
return {
name: this.name,
message: this.message,
code: this.getErrorCode(),
};
}
}
284 changes: 284 additions & 0 deletions src/api/decisions/validations/validations.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
import { ValidationService } from './validations.service';
import { ValidationError } from './validation.error';

describe('ValidationService', () => {
let validator: ValidationService;

beforeEach(() => {
validator = new ValidationService();
});

describe('validateInputs', () => {
it('should validate valid inputs', () => {
const ruleContent = {
fields: [
{ field: 'age', type: 'number-input' },
{ field: 'name', type: 'text-input' },
],
};
const context = { age: 25, name: 'John Doe' };
expect(() => validator.validateInputs(ruleContent, context)).not.toThrow();
});
});

describe('validateField', () => {
it('should validate number input', () => {
const field = { field: 'age', type: 'number-input' };
const context = { age: 25 };
expect(() => validator['validateField'](field, context)).not.toThrow();
});

it('should validate date input', () => {
const field = { field: 'birthDate', type: 'date' };
const context = { birthDate: '2000-01-01' };
expect(() => validator['validateField'](field, context)).not.toThrow();
});

it('should validate text input', () => {
const field = { field: 'name', type: 'text-input' };
const context = { name: 'John Doe' };
expect(() => validator['validateField'](field, context)).not.toThrow();
});

it('should validate boolean input', () => {
const field = { field: 'isActive', type: 'true-false' };
const context = { isActive: true };
expect(() => validator['validateField'](field, context)).not.toThrow();
});

it('should throw ValidationError for invalid input type', () => {
const field = { field: 'age', type: 'number-input' };
const context = { age: 'twenty-five' };
expect(() => validator['validateField'](field, context)).toThrow(ValidationError);
});
});

describe('validateNumberCriteria', () => {
it('should validate equal to criteria', () => {
const field = { field: 'age', type: 'number-input', validationType: '==', validationCriteria: '25' };
expect(() => validator['validateNumberCriteria'](field, 25)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 26)).toThrow(ValidationError);
});

it('should validate not equal to criteria', () => {
const field = { field: 'age', type: 'number-input', validationType: '!=', validationCriteria: '25' };
expect(() => validator['validateNumberCriteria'](field, 26)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 25)).toThrow(ValidationError);
});

it('should validate greater than or equal to criteria', () => {
const field = { field: 'age', type: 'number-input', validationType: '>=', validationCriteria: '18' };
expect(() => validator['validateNumberCriteria'](field, 18)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 20)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 17)).toThrow(ValidationError);
});

it('should validate greater than criteria', () => {
const field = { field: 'age', type: 'number-input', validationType: '>', validationCriteria: '18' };
expect(() => validator['validateNumberCriteria'](field, 19)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 17)).toThrow(ValidationError);
});

it('should validate less than or equal to criteria', () => {
const field = { field: 'age', type: 'number-input', validationType: '<=', validationCriteria: '18' };
expect(() => validator['validateNumberCriteria'](field, 18)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 17)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 19)).toThrow(ValidationError);
});

it('should validate less than criteria', () => {
const field = { field: 'age', type: 'number-input', validationType: '<', validationCriteria: '18' };
expect(() => validator['validateNumberCriteria'](field, 17)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 18)).toThrow(ValidationError);
});

it('should validate between exclusive criteria', () => {
const field = { field: 'age', type: 'number-input', validationType: '(num)', validationCriteria: '[16, 20]' };
expect(() => validator['validateNumberCriteria'](field, 17)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 19)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 16)).toThrow(ValidationError);
expect(() => validator['validateNumberCriteria'](field, 20)).toThrow(ValidationError);
expect(() => validator['validateNumberCriteria'](field, 21)).toThrow(ValidationError);
});

it('should validate between inclusive criteria', () => {
const field = { field: 'age', type: 'number-input', validationType: '[num]', validationCriteria: '[16, 20]' };
expect(() => validator['validateNumberCriteria'](field, 16)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 17)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 19)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 20)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 15)).toThrow(ValidationError);
expect(() => validator['validateNumberCriteria'](field, 21)).toThrow(ValidationError);
});

it('should validate multiple criteria', () => {
const field = {
field: 'age',
type: 'number-input',
validationType: '[=num]',
validationCriteria: '[18, 20]',
};
expect(() => validator['validateNumberCriteria'](field, 18)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 20)).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, 21)).toThrow(ValidationError);
});

it('should validate multiple criteria with multiple values', () => {
const field = {
field: 'age',
type: 'number-input',
validationType: '[=nums]',
validationCriteria: '[18, 20]',
};
expect(() => validator['validateNumberCriteria'](field, [18])).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, [18, 20])).not.toThrow();
expect(() => validator['validateNumberCriteria'](field, [19, 20])).toThrow(ValidationError);
});
});

describe('validateDateCriteria', () => {
it('should validate equal to criteria', () => {
const field = { field: 'date', type: 'date', validationType: '==', validationCriteria: '2023-01-01' };
expect(() => validator['validateDateCriteria'](field, '2023-01-01')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-01-02')).toThrow(ValidationError);
});

it('should validate not equal to criteria', () => {
const field = { field: 'date', type: 'date', validationType: '!=', validationCriteria: '2023-01-01' };
expect(() => validator['validateDateCriteria'](field, '2023-01-02')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-01-01')).toThrow(ValidationError);
});

it('should validate greater than criteria', () => {
const field = { field: 'date', type: 'date', validationType: '>', validationCriteria: '2023-01-01' };
expect(() => validator['validateDateCriteria'](field, '2023-01-02')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-01-01')).toThrow(ValidationError);
expect(() => validator['validateDateCriteria'](field, '2022-12-31')).toThrow(ValidationError);
});

it('should validate greater than or equal to criteria', () => {
const field = { field: 'date', type: 'date', validationType: '>=', validationCriteria: '2023-01-01' };
expect(() => validator['validateDateCriteria'](field, '2023-01-01')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-01-02')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2022-01-01')).toThrow(ValidationError);
});

it('should validate less than criteria', () => {
const field = { field: 'date', type: 'date', validationType: '<', validationCriteria: '2023-01-01' };
expect(() => validator['validateDateCriteria'](field, '2022-12-31')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-01-01')).toThrow(ValidationError);
expect(() => validator['validateDateCriteria'](field, '2023-01-02')).toThrow(ValidationError);
});

it('should validate less than or equal to criteria', () => {
const field = { field: 'date', type: 'date', validationType: '<=', validationCriteria: '2023-01-01' };
expect(() => validator['validateDateCriteria'](field, '2023-01-01')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-01-02')).toThrow(ValidationError);
});

it('should validate between exclusive criteria', () => {
const field = {
field: 'date',
type: 'date',
validationType: '(date)',
validationCriteria: '[2023-01-01, 2024-01-03]',
};
expect(() => validator['validateDateCriteria'](field, '2023-01-02')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2024-01-02')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-06-05')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2024-01-04')).toThrow(ValidationError);
expect(() => validator['validateDateCriteria'](field, '2022-12-31')).toThrow(ValidationError);
expect(() => validator['validateDateCriteria'](field, '2023-01-01')).toThrow(ValidationError);
});

it('should validate between inclusive criteria', () => {
const field = {
field: 'date',
type: 'date',
validationType: '[date]',
validationCriteria: '[2023-01-01, 2023-01-03]',
};
expect(() => validator['validateDateCriteria'](field, '2023-01-01')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-01-02')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-01-03')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-01-04')).toThrow(ValidationError);
expect(() => validator['validateDateCriteria'](field, '2022-12-31')).toThrow(ValidationError);
});

it('should validate multiple criteria with single dates', () => {
const field = {
field: 'date',
type: 'date',
validationType: '[=date]',
validationCriteria: '[2023-01-01, 2023-01-03]',
};
expect(() => validator['validateDateCriteria'](field, '2023-01-01')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-01-03')).not.toThrow();
expect(() => validator['validateDateCriteria'](field, '2023-01-02')).toThrow(ValidationError);
expect(() => validator['validateDateCriteria'](field, '2023-01-04')).toThrow(ValidationError);
});

it('should validate multiple criteria with multiple dates', () => {
const field = {
field: 'date',
type: 'date',
validationType: '[=dates]',
validationCriteria: '[2023-01-01, 2023-01-03]',
};
expect(() => validator['validateDateCriteria'](field, ['2023-01-01'])).not.toThrow();
expect(() => validator['validateDateCriteria'](field, ['2023-01-01', '2023-01-03'])).not.toThrow();
expect(() => validator['validateDateCriteria'](field, ['2023-01-01', '2023-01-02'])).toThrow(ValidationError);
expect(() => validator['validateDateCriteria'](field, ['2023-01-01', '2023-01-04'])).toThrow(ValidationError);
});
});

describe('validateTextCriteria', () => {
it('should validate equal to criteria', () => {
const field = { field: 'status', type: 'text-input', validationType: '==', validationCriteria: 'active' };
expect(() => validator['validateTextCriteria'](field, 'active')).not.toThrow();
expect(() => validator['validateTextCriteria'](field, 'inactive')).toThrow(ValidationError);
});

it('should validate not equal to criteria', () => {
const field = { field: 'status', type: 'text-input', validationType: '!=', validationCriteria: 'active' };
expect(() => validator['validateTextCriteria'](field, 'inactive')).not.toThrow();
expect(() => validator['validateTextCriteria'](field, 'active')).toThrow(ValidationError);
});

it('should validate regex criteria', () => {
const field = {
field: 'email',
type: 'text-input',
validationType: 'regex',
validationCriteria: '^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$',
};
expect(() => validator['validateTextCriteria'](field, '[email protected]')).not.toThrow();
expect(() => validator['validateTextCriteria'](field, 'invalid-email')).toThrow(ValidationError);
});

it('should validate multiple criteria with single text', () => {
const field = {
field: 'status',
type: 'text-input',
validationType: '[=text]',
validationCriteria: 'active,testing',
};
expect(() => validator['validateTextCriteria'](field, 'active')).not.toThrow();
expect(() => validator['validateTextCriteria'](field, 'inactive')).toThrow(ValidationError);
});

it('should validate multiple criteria with multiple texts', () => {
const field = {
field: 'status',
type: 'text-input',
validationType: '[=texts]',
validationCriteria: 'active,inactive',
};
expect(() => validator['validateTextCriteria'](field, ['active'])).not.toThrow();
expect(() => validator['validateTextCriteria'](field, ['active', 'inactive'])).not.toThrow();
expect(() => validator['validateTextCriteria'](field, ['active', 'inactive', 'testing'])).toThrow(
ValidationError,
);
});
});
});
Loading