Improve testing for shape validation
This commit is contained in:
parent
71cef6dee8
commit
8c19355486
|
@ -20,9 +20,9 @@ describe('Testing validation functionality', () => {
|
|||
expect(validateArray(testArrayStructure, ['a','b','c','d']))
|
||||
.toEqual(['a','b','c','d']);
|
||||
});
|
||||
it('Arrays should strip out primitives which fail the test (i.e. return undefined)', () => {
|
||||
it('Arrays should return undefined for primitives which fail the test', () => {
|
||||
expect(validateArray(testArrayStructure, ['a','b',3,'d']))
|
||||
.toEqual(['a','b','d']);
|
||||
.toEqual(['a','b',undefined,'d']);
|
||||
});
|
||||
|
||||
const testArrayStructure2 = Types.arrayOf(Types.shape({
|
||||
|
@ -65,4 +65,16 @@ describe('Testing validation functionality', () => {
|
|||
expect(validateObject(testObjectStructure2, { test1: { test1: 'toast', test2: 3 } }))
|
||||
.toEqual({ test1: { test1: 'toast', test2: 3 } });
|
||||
});
|
||||
const testObjectStructure3 = Types.shape({
|
||||
test1: Types.shape({
|
||||
test2: Types.string(),
|
||||
}),
|
||||
test2: Types.string(),
|
||||
});
|
||||
it('Objects containing objects should properly check if an object is provided', () => {
|
||||
expect(validateObject(testObjectStructure3, { test1: 'foo', test2: 'bar' })).toEqual({
|
||||
test1: undefined,
|
||||
test2: 'bar',
|
||||
});
|
||||
})
|
||||
});
|
||||
|
|
|
@ -13,7 +13,7 @@ export type ObjectReducerAction = {
|
|||
};
|
||||
export type ObjectReducerFactory = (reducerStructure: ShapeStructure) => ObjectReducer;
|
||||
export type ObjectReducer = (state: Object, action: ObjectReducerAction) => Object;
|
||||
export type ObjectReducerBehavior = (state: Object, payload: Object, initialState: Object) => Object;
|
||||
export type ObjectReducerBehavior = (state: Object, payload: Object | void, initialState: Object) => Object;
|
||||
export type ObjectReducerBehaviorsConfig = {
|
||||
[key: string]: {
|
||||
action?: (value: Object) => Object,
|
||||
|
@ -42,7 +42,7 @@ import { validateObject } from '../validatePayload';
|
|||
const DEFAULT_OBJECT_BEHAVIORS: ObjectReducerBehaviorsConfig = {
|
||||
update: {
|
||||
action(value) { return value },
|
||||
reducer(state, payload) {
|
||||
reducer(state, payload = {}) {
|
||||
return { ...state, ...payload };
|
||||
}
|
||||
},
|
||||
|
@ -53,7 +53,7 @@ const DEFAULT_OBJECT_BEHAVIORS: ObjectReducerBehaviorsConfig = {
|
|||
},
|
||||
replace: {
|
||||
action(value) { return value },
|
||||
reducer(state, payload) {
|
||||
reducer(state, payload = {}) {
|
||||
return payload;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,9 +12,12 @@ type validationFunction = (structure: StructureType | PrimitiveType | ShapeStruc
|
|||
import { reduce, isObject } from 'lodash';
|
||||
import { PROP_TYPES } from './structure';
|
||||
|
||||
export function validateObject(objectStructure: any, value: mixed): Object {
|
||||
if (!isObject(value) && !!value) console.error(`The value passed to validateObject() was not an object. Value: `, value);
|
||||
if (!isObject(value) || !value ) return {};
|
||||
export function validateObject(objectStructure: any, value: mixed): Object | void {
|
||||
if (!isObject(value) && !!value) {
|
||||
console.error(`The value passed to validateObject() was not an object. Value: `, value);
|
||||
return undefined;
|
||||
}
|
||||
if (!isObject(value) || !value ) return undefined;
|
||||
|
||||
return reduce(value, (memo, value, name) => {
|
||||
const valueType = objectStructure().structure[name];
|
||||
|
@ -51,8 +54,7 @@ export function validateArray(arrayStructure: any, value: Array<mixed>): Array<m
|
|||
//based on the provided reducer structure.
|
||||
const elementStructure = arrayStructure().structure;
|
||||
const elementType = elementStructure().type;
|
||||
return value.map(element => getTypeValidation(elementType)(elementStructure, element))
|
||||
.filter(v => v !== undefined);
|
||||
return value.map(element => getTypeValidation(elementType)(elementStructure, element));
|
||||
}
|
||||
|
||||
function getTypeValidation(type): validationFunction {
|
||||
|
|
Loading…
Reference in New Issue