1
0
Fork 0
mirror of synced 2024-09-20 11:27:56 +12:00
budibase/packages/builder/src/userInterface/propsDefinitionParsing/createDefaultProps.js

61 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-07-19 23:52:08 +12:00
import {
isString,
isUndefined
} from "lodash/fp";
2019-07-20 05:03:58 +12:00
import { types } from "./types";
2019-07-19 23:52:08 +12:00
import { assign } from "lodash";
export const createDefaultProps = (propsDefinition, derivedFromProps) => {
const props = {};
const errors = [];
for(let propDef in propsDefinition) {
const parsedPropDef = parsePropDef(propsDefinition[propDef]);
if(parsedPropDef.error)
errors.push({propName:propDef, error:parsedPropDef.error});
else
props[propDef] = parsedPropDef;
}
if(derivedFromProps) {
assign(props, ...derivedFromProps);
}
return ({
props, errors
});
}
const parsePropDef = propDef => {
2019-07-20 05:03:58 +12:00
const error = message => ({error:message, propDef});
2019-07-19 23:52:08 +12:00
if(isString(propDef)) {
if(!types[propDef])
return error(`Do not recognise type ${propDef}`);
return types[propDef].default();
}
if(!propDef.type)
return error("Property Definition must declare a type");
const type = types[propDef.type];
if(!type)
return error(`Do not recognise type ${propDef.type}`);
if(isUndefined(propDef.default))
return type.default(propDef);
if(!type.isOfType(propDef.default))
return error(`${propDef.default} is not of type ${type}`);
return propDef.default;
}
2019-07-20 05:03:58 +12:00
/*
Allowed propDefOptions
- type: string, bool, number, array
- default: default value, when undefined
- required: field is required
*/