@inkline/inkline
Version:
Inkline is the Vue.js UI/UX Library built for creating your next design system
36 lines (31 loc) • 1.06 kB
text/typescript
import {
defaultValidationValues,
reservedValidationFields,
defaultFieldValidationValues
} from '@inkline/inkline/constants';
/**
* Initialize raw form schema by adding required default fields if they do not exist
*
* @param schema
* @returns {*}
*/
export function initialize (schema: any) {
const isField = Object.keys(schema).length === 0 || Array.isArray(schema.validators) || schema.hasOwnProperty('value');
const defaultValues = isField
? { ...defaultValidationValues, ...defaultFieldValidationValues }
: defaultValidationValues;
Object.entries(defaultValues)
.forEach(([key, value]) => {
if (!schema.hasOwnProperty(key)) {
schema[key] = value;
}
});
Object.keys(schema)
.filter((key) => !reservedValidationFields.includes(key))
.forEach((key) => {
if (typeof schema[key] === 'object' || Array.isArray(schema[key])) {
schema[key] = initialize(schema[key]);
}
});
return schema;
}