|
| 1 | +function validateSchemaNames(schema, fieldsArray) { |
| 2 | + const fieldNames = fieldsArray.map((field) => field.field); |
| 3 | + const invalidNames = []; |
| 4 | + |
| 5 | + schema.forEach((item) => { |
| 6 | + if (item.name && !fieldNames.includes(item.name)) { |
| 7 | + invalidNames.push(item.name); |
| 8 | + } |
| 9 | + if (item.children && Array.isArray(item.children)) { |
| 10 | + item.children.forEach((child) => { |
| 11 | + if (child.name && !fieldNames.includes(child.name)) { |
| 12 | + invalidNames.push(child.name); |
| 13 | + } |
| 14 | + }); |
| 15 | + } |
| 16 | + }); |
| 17 | + |
| 18 | + if (invalidNames.length > 0) { |
| 19 | + throw new Error(`Form schema field names don't match collection field names: ${invalidNames.join(', ')}`); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +const form_schema = [ |
| 24 | + { |
| 25 | + name: 'first_name', |
| 26 | + type: 'text', |
| 27 | + label: 'First Name', |
| 28 | + placeholder: 'John', |
| 29 | + help: null, |
| 30 | + validation: 'required', |
| 31 | + width: '50', |
| 32 | + }, |
| 33 | + { |
| 34 | + name: 'last_name', |
| 35 | + type: 'text', |
| 36 | + label: 'Last Name', |
| 37 | + validation: 'required', |
| 38 | + width: '50', |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'email', |
| 42 | + type: 'text', |
| 43 | + label: 'Email', |
| 44 | + |
| 45 | + validation: 'required', |
| 46 | + width: '100', |
| 47 | + }, |
| 48 | + { |
| 49 | + name: 'organization', |
| 50 | + type: 'text', |
| 51 | + label: 'Company', |
| 52 | + help: `What's the name of your company / organization?`, |
| 53 | + width: '100', |
| 54 | + conditionalIf: '$get(first_name).value', |
| 55 | + }, |
| 56 | + { |
| 57 | + name: 'signature', |
| 58 | + type: 'signature', |
| 59 | + label: 'Signature', |
| 60 | + help: `Please sign your name above.`, |
| 61 | + width: '100', |
| 62 | + validation: 'required', |
| 63 | + options: ['type', 'draw', 'upload'], |
| 64 | + }, |
| 65 | + { |
| 66 | + name: 'esignature_agreement', |
| 67 | + type: 'checkbox', |
| 68 | + label: 'I agree that my electronic signature is as valid and legally binding as a handwritten signature.', |
| 69 | + validation: 'required', |
| 70 | + width: '100', |
| 71 | + }, |
| 72 | +]; |
| 73 | + |
| 74 | +module.exports = async function (data) { |
| 75 | + validateSchemaNames(form_schema, data.get_fields); |
| 76 | + |
| 77 | + return; |
| 78 | +}; |
0 commit comments