Address flow errors
This commit is contained in:
parent
6ff5c56a2a
commit
69a0d3c616
|
@ -2,7 +2,7 @@
|
|||
//==============================
|
||||
// Flow imports
|
||||
//==============================
|
||||
import type { StructureType } from '../structure';
|
||||
import type { StructureType, ArrayStructureType } from '../structure';
|
||||
|
||||
//==============================
|
||||
// Flow types
|
||||
|
@ -95,17 +95,17 @@ const DEFAULT_ARRAY_BEHAVIORS: ArrayReducerBehaviorsConfig = {
|
|||
};
|
||||
|
||||
|
||||
export function createArrayReducer(reducerShape: StructureType, {
|
||||
export function createArrayReducer(arrayTypeDescription: ArrayStructureType, {
|
||||
locationString
|
||||
}: ArrayReducerOptions = {}) {
|
||||
return {
|
||||
reducers: createReducer(reducerShape, createReducerBehaviors(DEFAULT_ARRAY_BEHAVIORS, locationString)),
|
||||
reducers: createReducer(arrayTypeDescription, createReducerBehaviors(DEFAULT_ARRAY_BEHAVIORS, locationString)),
|
||||
actionsObject: createActions(DEFAULT_ARRAY_BEHAVIORS, locationString, {}),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function createReducer(arrayTypeDescription: StructureType, behaviors: ArrayReducerBehaviors): ArrayReducer {
|
||||
function createReducer(arrayTypeDescription: ArrayStructureType, behaviors: ArrayReducerBehaviors): ArrayReducer {
|
||||
//Take the initial value specified as the default for the array, then apply it, using the validation
|
||||
//when doing so. The initial value must be an array.
|
||||
const initialValue = validateArray(arrayTypeDescription, arrayTypeDescription().defaultValue);
|
||||
|
@ -122,7 +122,7 @@ function createReducer(arrayTypeDescription: StructureType, behaviors: ArrayRedu
|
|||
}
|
||||
|
||||
|
||||
function applyValidation(arrayTypeDescription: StructureType, payload: any) {
|
||||
function applyValidation(arrayTypeDescription: ArrayStructureType, payload: any) {
|
||||
// Array validation is more tricky than object/primitive, as it is possible that the current
|
||||
// action may involve updating the contents of a specific array element, rather than the
|
||||
// whole array. As a result, some extra functionality is required to determine which
|
||||
|
|
|
@ -82,7 +82,7 @@ function calculateDefaults(reducerStructure) {
|
|||
|
||||
|
||||
function createReducer(objectStructure: StructureType, behaviors: ObjectReducerBehaviors): ObjectReducer {
|
||||
const initialState = validateObject(objectStructure, calculateDefaults(objectStructure().structure));
|
||||
const initialState: Object = validateObject(objectStructure, calculateDefaults(objectStructure().structure));
|
||||
return (state = initialState, { type, payload }: ObjectReducerAction) => {
|
||||
//If the action type does not match any of the specified behaviors, just return the current state.
|
||||
if (!behaviors[type]) return state;
|
||||
|
|
|
@ -11,6 +11,11 @@ export type StructureType = () => {
|
|||
structure: ShapeStructure | StructureType | PrimitiveType,
|
||||
defaultValue?: any,
|
||||
};
|
||||
export type ArrayStructureType = () => {
|
||||
type: string,
|
||||
structure: StructureType | PrimitiveType,
|
||||
defaultValue: any,
|
||||
}
|
||||
export type PrimitiveType = () => {
|
||||
type: $Keys<typeof PROP_TYPES>,
|
||||
defaultValue?: any,
|
||||
|
|
|
@ -12,12 +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 | void {
|
||||
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);
|
||||
return undefined;
|
||||
return {};
|
||||
}
|
||||
if (!isObject(value) || !value ) return undefined;
|
||||
if (!isObject(value) || !value ) return {};
|
||||
|
||||
return reduce(value, (memo, value, name) => {
|
||||
const valueType = objectStructure().structure[name];
|
||||
|
@ -44,13 +44,13 @@ export function validateObject(objectStructure: any, value: mixed): Object | voi
|
|||
}, {});
|
||||
}
|
||||
|
||||
export function validatePrimitive(primitive: any, value: mixed): mixed {
|
||||
export function validatePrimitive(primitive: any, value: any): mixed {
|
||||
//Validate primitives using the typeofValue property of the primitive type definitions.
|
||||
if (typeof value === primitive().typeofValue ) return value;
|
||||
return console.warn(`The value, ${value}, did not match the type specified (${primitive().type}).`);
|
||||
}
|
||||
|
||||
export function validateArray(arrayStructure: any, value: Array<mixed>): Array<mixed> {
|
||||
export function validateArray(arrayStructure: any, value: Array<any> | void): Array<mixed> {
|
||||
//Validate arrays by performing either of the other validation types to each element of the array,
|
||||
//based on the provided reducer structure.
|
||||
if (!Array.isArray(value)) return [];
|
||||
|
|
Loading…
Reference in New Issue