Skip to content

Commit

Permalink
Merge pull request #4 from freeman-industries/input-on-error
Browse files Browse the repository at this point in the history
Include original input in thrown error
  • Loading branch information
nabilfreeman authored Aug 17, 2023
2 parents a19942b + 25e0d1f commit bca0c04
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class Check {
// remove last character from concatenated error message.
error_message = error_message.slice(0, -1);

const error = new CheckError(error_message, this.schema);
const error = new CheckError(error_message, this.schema, object);

throw error;
}
Expand Down
14 changes: 13 additions & 1 deletion src/CheckError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { CheckError } from './CheckError';
import { Schema } from 'jsonschema';

describe('CheckError', () => {
const schema: Schema = { type: 'object' };
const input = 'something';

it('should set the message property correctly', () => {
const error = new CheckError('Test error message');
expect(error.message).to.equal('Test error message');
Expand All @@ -19,7 +22,6 @@ describe('CheckError', () => {
});

it('should handle the schema property when provided', () => {
const schema: Schema = { type: 'object' };
const error = new CheckError('Test error message', schema);
expect(error.schema).to.deep.equal(schema);
});
Expand All @@ -28,4 +30,14 @@ describe('CheckError', () => {
const error = new CheckError('Test error message');
expect(error.schema).to.be.undefined;
});

it('should handle the input property when provided', () => {
const error = new CheckError('Test error message', schema, input);
expect(error.input).to.equal(input);
});

it('should handle the input property when omitted', () => {
const error = new CheckError('Test error message');
expect(error.input).to.be.undefined;
});
});
11 changes: 10 additions & 1 deletion src/CheckError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ export class CheckError extends Error {
*/
schema: Schema | undefined;

/**
* The input that caused the validation error.
*
* @type {any}
*/
input: any;

/**
* Creates a new instance of CheckError.
*
* @param {string} message - The error message describing the validation failure.
* @param {Schema} [schema] - The schema that failed to validate (optional).
* @param {any} [input] - The input that caused the validation error.
*/
constructor(message: string, schema?: Schema) {
constructor(message: string, schema?: Schema, input?: any) {
super(message);
this.name = 'CheckError';
this.schema = schema;
this.input = input;
}
}

0 comments on commit bca0c04

Please sign in to comment.