Skip to content

Commit

Permalink
Merge pull request #16 from czajkowski/dynamic-error-messages
Browse files Browse the repository at this point in the history
Custom validators allow to define a function that will calculate error messages during validation
  • Loading branch information
mprzodala authored Oct 16, 2018
2 parents 9e50969 + 74be418 commit 25a40cd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,16 @@ class Schema {
this.errors[key] = [];
}

const errorMessage = typeof error === 'function' ?
error() :
error;

if (index > -1) {
this.errors[key][index] = mergeErrors(this.errors[key][index], error);
this.errors[key][index] = mergeErrors(this.errors[key][index], errorMessage);
return;
}

this.errors[key].push(error);
this.errors[key].push(errorMessage);
}

setModelError(path, message) {
Expand Down
34 changes: 33 additions & 1 deletion src/Schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ describe('Schema', () => {
expect(Object.keys(modelWithNumberErrors).length).toBe(7);
});
});

describe('Should validate using custom validators', () => {
jest.useFakeTimers();

Expand Down Expand Up @@ -898,7 +899,6 @@ describe('Schema', () => {
expect(Object.keys(testObject2Errors).length).toBe(1);
});


it('async with promise (1 error)', () => {
const asyncValidator = () => ({
validator(value) {
Expand Down Expand Up @@ -990,6 +990,29 @@ describe('Schema', () => {
const results = schema.validate({ foo: 'foo' });
expect(results.foo).toEqual([errorMessage]);
});

it('should allow to define a validator errorMessage as a function that will return an error during validation', () => {
const customErrorMessage = 'foo error';
const customValidator = {
validator: () => false,
errorMessage: () => customErrorMessage,
};

const schema = new Schema({
property: {
type: String,
validators: [customValidator],
},
});

const testObject = {
property: 'value',
};

expect(schema.validate(testObject)).toEqual({
property: [customErrorMessage],
});
});
});

describe('Default value', () => {
Expand Down Expand Up @@ -1522,6 +1545,7 @@ describe('Schema', () => {
expect(schema.getDefaultValues()).toEqual({ foo: 'foo', bar: '' });
});
});

describe('additionalValidators', () => {
it('should add additional validator', () => {
const fooValidator = jest.fn();
Expand All @@ -1530,13 +1554,15 @@ describe('Schema', () => {
schema.addValidator(fooValidator);
expect(schema.additionalValidators.size).toEqual(1);
});

it('should not add the validator if is not a function', () => {
const fooValidator = 'foo';
const schema = new Schema({});

schema.addValidator(fooValidator);
expect(schema.additionalValidators.size).toEqual(0);
});

it('should remove additional validator', () => {
const fooValidator = jest.fn();
const schema = new Schema({});
Expand All @@ -1545,6 +1571,7 @@ describe('Schema', () => {
schema.removeValidator(fooValidator);
expect(schema.additionalValidators.size).toEqual(0);
});

it('should set error on field in first layer of model', () => {
const modelSchema = new Schema({
foo: {
Expand All @@ -1562,6 +1589,7 @@ describe('Schema', () => {

expect(modelSchema.validate(data)).toEqual({ foo: ['foo error message!'] });
});

it('should set error on field in second layer of model', () => {
const fooSchema = new Schema({
fooStart: {
Expand Down Expand Up @@ -1592,6 +1620,7 @@ describe('Schema', () => {
],
});
});

it('should set error on field in third layer of model', () => {
const fooBarSchema = new Schema({
bar1: {
Expand Down Expand Up @@ -1635,6 +1664,7 @@ describe('Schema', () => {
],
});
});

it('should set error on field in third layer of model in specific element in array', () => {
const fooBarSchema = new Schema({
bar1: {
Expand Down Expand Up @@ -1685,6 +1715,7 @@ describe('Schema', () => {
],
});
});

it('should merge errors if error is set on the same key and index', () => {
const unitSchema = new Schema({
value: {
Expand Down Expand Up @@ -1811,6 +1842,7 @@ describe('Schema', () => {
],
});
});

it('should set error on field (async validation)', () => {
const fooSchema = new Schema({
fooStart: {
Expand Down

0 comments on commit 25a40cd

Please sign in to comment.