q42-cms-components
Version:
48 lines (45 loc) • 1.25 kB
JavaScript
export function initialValue(schema, isProp) {
if (schema.enum) {
return schema.enum[0];
}
if (schema.$ref) {
return null;
}
switch (schema.type) {
case 'string':
if (!schema.required)
return null;
switch (schema.format) {
case 'datetime-picker':
return new Date();
default:
return '';
}
case 'integer':
case 'number':
return 0;
case 'boolean':
return false;
case 'null':
return null;
case 'array':
return schema.minLength && !schema.items.oneOf
? Array(schema.minLength).fill(0).map(() => initialValue(schema.items))
: [];
default:
if (!schema.properties)
throw `Missing initialiser for type ${schema.type}`;
if (!schema.required && isProp)
return null;
if (schema.oneOf && isProp)
return null;
var value = { $type: null };
for (var p in schema.properties) {
value[p] = initialValue(schema.properties[p], true);
}
return value;
}
}
export function isSimpleProp(schema) {
return !(schema.type == 'object' || (schema.type == 'array' && schema.items.type == 'object'));
}