Skip to content

Commit

Permalink
labels in errors and date type default value fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Przodała committed Sep 8, 2017
1 parent 17c4351 commit d4a57ae
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
34 changes: 20 additions & 14 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import {
import OneOfTypes from './OneOfTypes';

const defaultMessages = {
notDefinedKey(key) { return `Key '${key}' is not defined in schema`; },
notDefinedKey(fieldName) { return `Key '${fieldName}' is not defined in schema`; },
modelIsUndefined() { return 'Validated model is undefined'; },
validateRequired(key) { return `Field '${key}' is required`; },
validateString(key) { return `Field '${key}' is not a String`; },
validateNumber(key) { return `Field '${key}' is not a Number`; },
validateObject(key) { return `Field '${key}' is not a Object`; },
validateArray(key) { return `Field '${key}' is not a Array`; },
validateBoolean(key) { return `Field '${key}' is not a Boolean`; },
validateDate(key) { return `Field '${key}' is not a Date`; },
validateRequired(fieldName) { return `Field '${fieldName}' is required`; },
validateString(fieldName) { return `Field '${fieldName}' is not a String`; },
validateNumber(fieldName) { return `Field '${fieldName}' is not a Number`; },
validateObject(fieldName) { return `Field '${fieldName}' is not a Object`; },
validateArray(fieldName) { return `Field '${fieldName}' is not a Array`; },
validateBoolean(fieldName) { return `Field '${fieldName}' is not a Boolean`; },
validateDate(fieldName) { return `Field '${fieldName}' is not a Date`; },
};

class Schema {
Expand Down Expand Up @@ -267,37 +267,43 @@ class Schema {

validateTypeString(value, key, index) {
if (typeof value === 'string') return true;
this.setError(key, this.messages.validateString(key), index);
const { label } = this.getField(key);
this.setError(key, this.messages.validateString(label || key), index);
return false;
}

validateTypeNumber(value, key, index) {
if (typeof value === 'number') return true;
this.setError(key, this.messages.validateNumber(key), index);
const { label } = this.getField(key);
this.setError(key, this.messages.validateNumber(label || key), index);
return false;
}

validateTypeObject(value, key, index) {
if (typeof value === 'object' && !Array.isArray(value) && value !== null) return true;
this.setError(key, this.messages.validateObject(key), index);
const { label } = this.getField(key);
this.setError(key, this.messages.validateObject(label || key), index);
return false;
}

validateTypeArray(value, key, index) {
if (Array.isArray(value)) return true;
this.setError(key, this.messages.validateArray(key), index);
const { label } = this.getField(key);
this.setError(key, this.messages.validateArray(label || key), index);
return false;
}

validateTypeBoolean(value, key, index) {
if (typeof value === 'boolean') return true;
this.setError(key, this.messages.validateBoolean(key), index);
const { label } = this.getField(key);
this.setError(key, this.messages.validateBoolean(label || key), index);
return false;
}

validateTypeDate(value, key, index) {
if (value instanceof Date) return true;
this.setError(key, this.messages.validateDate(key), index);
const { label } = this.getField(key);
this.setError(key, this.messages.validateDate(label || key), index);
return false;
}

Expand Down
25 changes: 25 additions & 0 deletions src/Schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,31 @@ describe('Schema', () => {
expect(Object.keys(errors2).length).toBe(0);
});

it('should display label in error message if is defined in schema unless display key', () => {
const schemaObject = {
foo: {
type: String,
label: 'testLabel',
},
bar: {
type: String,
},
};
const schema = new Schema(schemaObject);

const data = {
foo: 10,
bar: 12,
};

const errors = schema.validate(data);

expect(errors).toEqual({
foo: ['Field \'testLabel\' is not a String'],
bar: ['Field \'bar\' is not a String'],
});
});

it('should pick fields from schema', () => {
const schema = new Schema({
foo: {
Expand Down
3 changes: 3 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const getDefaultValueForType = (type, isArrayOfType) => {
if (type === Number) {
return wrapToArray(NaN, isArrayOfType);
}
if (type === Date) {
return wrapToArray(new Date(), isArrayOfType);
}
return wrapToArray(type(), isArrayOfType);
};

Expand Down
25 changes: 16 additions & 9 deletions src/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,23 @@ describe('helpers', () => {
});

it('should get default value for type', () => {
const foo = Number;
const bar = String;
const foo3 = { getDefaultValue: () => 'bar' };
const number = Number;
const string = String;
const boolean = Boolean;
const date = Date;
const custom = { getDefaultValue: () => 'bar' };

expect(isNaN(getDefaultValueForType(foo))).toEqual(true);
expect(getDefaultValueForType(bar)).toEqual('');
expect(getDefaultValueForType(foo3)).toEqual('bar');
expect(isNaN(getDefaultValueForType(foo, true)[0])).toEqual(true);
expect(getDefaultValueForType(bar, true)).toEqual(['']);
expect(getDefaultValueForType(foo3, true)).toEqual(['bar']);
expect(isNaN(getDefaultValueForType(number))).toEqual(true);
expect(getDefaultValueForType(string)).toEqual('');
expect(getDefaultValueForType(boolean)).toEqual(false);
expect(getDefaultValueForType(custom)).toEqual('bar');
expect(getDefaultValueForType(date) instanceof Date).toEqual(true);

expect(isNaN(getDefaultValueForType(number, true)[0])).toEqual(true);
expect(getDefaultValueForType(string, true)).toEqual(['']);
expect(getDefaultValueForType(boolean, true)).toEqual([false]);
expect(getDefaultValueForType(custom, true)).toEqual(['bar']);
expect(getDefaultValueForType(date, true)[0] instanceof Date).toEqual(true);
});

it('should get default value from options', () => {
Expand Down

0 comments on commit d4a57ae

Please sign in to comment.