Add the 'any' Type for miscellaneous/uncheck values

This commit is contained in:
Kai Moseley 2016-12-22 11:20:40 +00:00
parent 70fc54d280
commit ce24facd5a
5 changed files with 20 additions and 14 deletions

View File

@ -26,6 +26,11 @@
"/src"
]
},
"repository" :
{
"type" : "git",
"url" : "https://github.com/npm/npm.git"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.18.2",

View File

@ -18,6 +18,10 @@ describe('Validation functionality', () => {
it('Boolean primitive should allow for string', () => {
expect(validatePrimitive(Types.boolean(), true)).toBe(true);
});
it('Any should allow for anything', () => {
const date = new Date();
expect(validatePrimitive(Types.any(), date)).toEqual(date);
})
});
describe('Arrays', () => {

View File

@ -48,6 +48,7 @@ export const REDUCER_CREATOR_MAPPING: { [key: PropTypeKeys]: any } = {
[PROP_TYPES._boolean]: createPrimitiveReducer,
[PROP_TYPES._string]: createPrimitiveReducer,
[PROP_TYPES._number]: createPrimitiveReducer,
[PROP_TYPES._any]: createPrimitiveReducer,
};

View File

@ -29,19 +29,8 @@ export type PrimitiveType = () => {
structure?: PrimitiveType,
};
export type TypesObject = {
[key: string]: CreateArrayType | CreateStringType | CreateNumberType | CreateShapeType | CreateBooleanType;
}
export type TypesObjectDefaults = {
[key: string]: mixed | TypesArrayDefaults,
}
export type TypesArrayDefaults = Array<mixed> | Array<TypesObjectDefaults>;
type CreateStringType = (defaultValue: string) => PrimitiveType;
type CreateNumberType = (defaultValue: number) => PrimitiveType;
type CreateBooleanType = (defaultValue: boolean) => PrimitiveType;
type CreateArrayType = (structure: StructureType | PrimitiveType, defaultValue: TypesArrayDefaults | TypesObjectDefaults) => StructureType;
type CreateShapeType = (structure: ShapeStructure, defaultValue: TypesArrayDefaults | TypesObjectDefaults) => StructureType;
[key: string]: any,
};
//==============================
// Structure
@ -53,6 +42,7 @@ export const PROP_TYPES = {
_reducer: '_reducer',
_shape: '_shape',
_array: '_array',
_any: '_any',
};
//The types objects are used in order to build up the structure of a store chunk, and provide/accept
@ -73,6 +63,11 @@ export const Types: TypesObject = {
defaultValue,
typeofValue: 'boolean',
}),
any: (defaultValue: any = null) => () => ({
type: PROP_TYPES._any,
defaultValue,
typeofValue: 'any',
}),
arrayOf: (structure: StructureType | PrimitiveType, defaultValue = []) => () => ({
type: PROP_TYPES._array,
structure,

View File

@ -46,7 +46,7 @@ export function validateShape(objectStructure: any, value: mixed): Object {
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;
if (typeof value === primitive().typeofValue || primitive().typeofValue === 'any') return value;
return console.warn(`The value, ${value}, did not match the type specified (${primitive().type}).`);
}
@ -71,6 +71,7 @@ export function getTypeValidation(type: string): validationFunction {
[PROP_TYPES._boolean]: validatePrimitive,
[PROP_TYPES._array]: validateArray,
[PROP_TYPES._shape]: validateShape,
[PROP_TYPES._any]: validatePrimitive,
};
const typeValidation = TYPE_VALIDATIONS[type];
if (!typeValidation) {