Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 6x 6x 6x 155x 155x 2x 153x 157x 157x 7x 146x 146x 5x 141x 6x | const { validateFieldDefinition } = require('./schemas/field-definition-schema')
const { MAX_ENTITY_FIELDS } = require('./constants')
const { findFieldNameCollision } = require('./fieldNameUniqueness')
/**
* Validates the shape of every field definition in an entity's fields array:
* known `type`, well-formed `options` for select/multiselect, sound
* min/max/step for number fields, positive length/maxLines constraints — and
* that the array itself isn't unbounded (see MAX_ENTITY_FIELDS). Two fields
* cannot share a name in one language (see fieldNameUniqueness.js): only a
* collision the fields introduce since `previousFields` is refused, so an
* entity that already holds one stays editable.
*
* @param {Array} fields - The entity fields array from the request body
* @param {{ previousFields?: Array, defaultLanguage?: string, previousDefaultLanguage?: string }} [options]
* @returns {{ valid: boolean, errorResponse?: object }}
*/
function validateFieldDefinitions(fields, options = {}) {
Iif (!Array.isArray(fields)) return { valid: true }
if (fields.length > MAX_ENTITY_FIELDS) {
return {
valid: false,
errorResponse: {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'TOO_MANY_FIELDS', max: MAX_ENTITY_FIELDS })
}
}
}
for (const field of fields) {
const result = validateFieldDefinition(field)
if (!result.valid) {
return {
valid: false,
errorResponse: {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: result.code, fieldId: field.fieldId })
}
}
}
}
const collision = findFieldNameCollision(fields, options)
if (collision) {
return {
valid: false,
errorResponse: {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'FIELD_NAME_TAKEN', ...collision })
}
}
}
return { valid: true }
}
module.exports = { validateFieldDefinitions }
|