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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | 11x 11x 11x 81x 70x 45x 26x 26x 173x 356x 81x 12x 161x 112x 87x 41x 46x 161x 331x 112x 112x 112x 66x 5x 107x 4x 4x 4x 2x 154x 154x 11x | const { validateItemDataTypes } = require('../utils/schemas/item-data-schema')
// System-wide fallback ceilings applied when a text/textarea/email field
// definition omits its own `length` — previously unbounded in that case (see
// docs/architecture/USAGE_METERING.md "Bounding storage before it can be
// priced"). An entity designer who sets their own `length` keeps using it;
// these only backstop fields that never set one.
const DEFAULT_TEXT_FIELD_MAX_LENGTH = 500
const DEFAULT_TEXTAREA_MAX_LENGTH = 20000
// Returns true when a field value should be treated as "not provided"
function isEmptyValue(value) {
if (value === undefined || value === null || value === '') return true
if (typeof value === 'object' && !Array.isArray(value)) {
return Object.keys(value).length === 0 || Object.values(value).every((v) => !v || v === '')
}
Iif (Array.isArray(value)) return value.length === 0
return false
}
// Validates mandatory fields against `data`, a merged view of the item's final
// state (existing values overridden by whatever is present in the incoming
// request payload). boolean/workflow types are never required: booleans are
// always present (true/false), and workflow fields are auto-assigned server-side.
function validateMandatoryFields(fields, data) {
for (const field of fields) {
if (field.mandatory && !['boolean', 'workflow'].includes(field.type)) {
if (isEmptyValue(data[field.fieldId])) {
return { valid: false, fieldId: field.fieldId }
}
}
}
return { valid: true }
}
// Returns the plain string values held by a field value, regardless of whether
// it's a single string or a multilingual { lang: string } object.
function getStringValues(value) {
if (typeof value === 'string') return [value]
if (value && typeof value === 'object' && !Array.isArray(value)) {
return Object.values(value).filter((v) => typeof v === 'string')
}
return []
}
// Validates max length and (for textarea) max line count constraints across
// all languages of a field's value. Mirrors validateMandatoryFields's shape
// so callers can short-circuit on the first violation found.
function validateFieldConstraints(fields, data) {
for (const field of fields) {
if (!['text', 'textarea', 'email'].includes(field.type)) continue
const values = getStringValues(data[field.fieldId])
const maxLength =
field.length ||
(field.type === 'textarea' ? DEFAULT_TEXTAREA_MAX_LENGTH : DEFAULT_TEXT_FIELD_MAX_LENGTH)
for (const value of values) {
if (value.length > maxLength) {
return { valid: false, fieldId: field.fieldId, error: 'FIELD_TOO_LONG' }
}
}
if (field.type === 'textarea' && field.maxLines) {
for (const value of values) {
const lineCount = (value.match(/\n/g) || []).length + 1
if (lineCount > field.maxLines) {
return { valid: false, fieldId: field.fieldId, error: 'FIELD_TOO_MANY_LINES' }
}
}
}
}
return { valid: true }
}
// Validates type/shape correctness of `data` against the entity's field
// definitions: number values are actual numbers (and within min/max), dates
// are well-formed, select/multiselect values are among the field's options,
// booleans are actual booleans. Mirrors validateMandatoryFields/
// validateFieldConstraints's shape so callers can short-circuit the same way.
// Presence is not checked here — that's validateMandatoryFields's job.
function validateFieldTypes(fields, data) {
return validateItemDataTypes(fields, data)
}
module.exports = {
isEmptyValue,
validateMandatoryFields,
validateFieldConstraints,
validateFieldTypes
}
|