(rootSchema: JSONSchema7, formData: any)
| 48 | }; |
| 49 | |
| 50 | export const validateMagicForm = (rootSchema: JSONSchema7, formData: any) => { |
| 51 | let validationError: any = null; |
| 52 | const propertyErrors: any[] = []; |
| 53 | try { |
| 54 | if (rootSchema.type === 'object') { |
| 55 | // console.log(Object.keys(rootSchema.properties!)); |
| 56 | if (typeof formData !== 'object') { |
| 57 | propertyErrors.push({ |
| 58 | instancePath: '', |
| 59 | schemaPath: '#/type', |
| 60 | keyword: 'type', |
| 61 | params: { type: 'object' }, |
| 62 | message: 'must be object' |
| 63 | }); |
| 64 | } else { |
| 65 | Object.keys(rootSchema.properties!).forEach(propertyKey => { |
| 66 | const propertySchema = rootSchema.properties![propertyKey] as JSONSchema7; |
| 67 | const isRequired = rootSchema.required && rootSchema.required.indexOf(propertyKey) !== -1; |
| 68 | const propertyValue = getObjectOperandProperty(formData, propertyKey, propertySchema); |
| 69 | // console.log('validateFormData', { |
| 70 | // propertyKey, propertySchema, propertyValue, isRequired, |
| 71 | // rootSchema, formData, |
| 72 | // }); |
| 73 | if (isRequired) { |
| 74 | if (isOperandNullValue(propertyValue, propertySchema)) { |
| 75 | propertyErrors.push({ |
| 76 | dataPath: `.${propertyKey}`, |
| 77 | keyword: 'required', |
| 78 | message: t(Strings.robot_config_empty_warning), |
| 79 | params: { missingProperty: propertyKey }, |
| 80 | schemaPath: '#/required', |
| 81 | }); |
| 82 | } else { |
| 83 | if (propertySchema.enum) { |
| 84 | if (isLiteralOperand(propertyValue) && !propertySchema.enum!.includes(getLiteralOperandValue(propertyValue))) { |
| 85 | propertyErrors.push({ |
| 86 | dataPath: `.${propertyKey}`, |
| 87 | keyword: 'enum', |
| 88 | message: t(Strings.robot_config_empty_warning), |
| 89 | params: { enum: propertySchema.enum }, |
| 90 | schemaPath: '#/enum', |
| 91 | }); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } else { |
| 96 | if (propertySchema.enum) { |
| 97 | if (isLiteralOperand(propertyValue) && !propertySchema.enum!.includes(getLiteralOperandValue(propertyValue))) { |
| 98 | propertyErrors.push({ |
| 99 | dataPath: `.${propertyKey}`, |
| 100 | keyword: 'enum', |
| 101 | message: 'The specified option does not exist', // TODO: i18n |
| 102 | params: { enum: propertySchema.enum }, |
| 103 | schemaPath: '#/enum', |
| 104 | }); |
| 105 | } |
| 106 | } |
| 107 | if (isOperandNullValue(propertyValue, propertySchema)) { |
no test coverage detected