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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | 6x 7x 4x 4x 3x 3x 1x 26x 26x 36x 36x 7x 5x 26x 6x 3x 3x 3x 5x 5x 3x 3x 3x 2x 2x 2x 2x 2x 2x 7x 7x 7x 6x 2x 2x 4x 1x 4x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 21x 21x 30x 30x 7x 7x 3x 3x 3x 3x 1x 1x 16x 21x 21x 21x 21x 7x 8x 8x 7x 8x 21x | // Client-side mirror of flows-api/infrastructure/lambda/utils/schemas/item-data-schema.js
// and flows-api/infrastructure/lambda/items/field-validation.js — keep the two in sync.
// This only checks presence (mandatory) and type/shape correctness of item field
// values before submit; it is a UX convenience, not a security boundary — the
// backend re-validates everything and remains the source of truth.
import { z } from 'zod'
import type { EntityField } from '@/stores/entities'
// fieldId -> i18n key (under useUILanguage's t()), not a pre-translated string —
// the consuming component resolves the field's display name and calls t() itself.
export type FieldErrorMap = Record<string, string>
// Maps both client-side validation results and backend error codes to the same
// i18n keys, so a rule caught early (client) or late (server) reads identically.
export const ITEM_ERROR_CODE_TO_I18N_KEY: Record<string, string> = {
MANDATORY_FIELD_MISSING: 'item.fieldRequired',
UNIQUE_FIELD_CONFLICT: 'item.fieldUnique',
FIELD_TOO_LONG: 'item.fieldTooLong',
FIELD_TOO_MANY_LINES: 'item.fieldTooManyLines',
INVALID_REFERENCE_VALUE: 'item.fieldInvalidReference',
MISSING_REFERENCE_ENTITY: 'item.fieldInvalidReference',
REFERENCE_MIN_VALUES: 'item.fieldReferenceMinValues',
REFERENCE_MAX_VALUES: 'item.fieldReferenceMaxValues',
INVALID_NUMBER_VALUE: 'item.fieldInvalidNumber',
FIELD_OUT_OF_RANGE: 'item.fieldOutOfRange',
INVALID_DATE_FORMAT: 'item.fieldInvalidDate',
INVALID_SELECT_VALUE: 'item.fieldInvalidSelectValue',
INVALID_FIELD_VALUE: 'item.fieldInvalidValue'
}
function isEmptyValue(value: unknown): boolean {
if (value === undefined || value === null || value === '') return true
Iif (Array.isArray(value)) return value.length === 0
if (typeof value === 'object') {
const values = Object.values(value as Record<string, unknown>)
return values.length === 0 || values.every((v) => !v || v === '')
}
return false
}
// Mirrors validateMandatoryFields in field-validation.js: boolean/workflow are
// never required (booleans always have a value, workflow is server-assigned).
export function validateMandatoryFields(
fields: EntityField[],
data: Record<string, unknown>
): FieldErrorMap {
const errors: FieldErrorMap = {}
for (const field of fields) {
Iif (!field.fieldId) continue
if (!field.mandatory || ['boolean', 'workflow'].includes(field.type || '')) continue
if (isEmptyValue(data[field.fieldId])) {
errors[field.fieldId] = 'item.fieldRequired'
}
}
return errors
}
// Same sentinel as flows-api/infrastructure/lambda/utils/deletedPlaceholder.js —
// values already resolved to "removed option" by an entity edit cascade should
// not be re-flagged as invalid on the next save.
const DELETED_PLACEHOLDER_PREFIX = '::deleted::'
function isDeletedPlaceholder(value: unknown): boolean {
return typeof value === 'string' && value.startsWith(DELETED_PLACEHOLDER_PREFIX)
}
function normalizeOptionValues(options: EntityField['options']): string[] {
Iif (!Array.isArray(options)) return []
return options
.map((opt) => (typeof opt === 'string' ? opt : opt?.value))
.filter((v): v is string => typeof v === 'string')
}
// Rejects calendar-invalid dates (e.g. 2024-02-30) that `new Date(...)` would
// otherwise silently roll over to the following month.
function isValidDateString(value: unknown): boolean {
Iif (typeof value !== 'string') return false
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value)
if (!match) return false
const year = Number(match[1])
const month = Number(match[2])
const day = Number(match[3])
Iif (month < 1 || month > 12 || day < 1 || day > 31) return false
const date = new Date(Date.UTC(year, month - 1, day))
return (
date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day
)
}
function addCustomIssue(ctx: z.RefinementCtx, code: string) {
ctx.addIssue({ code: z.ZodIssueCode.custom, params: { code } })
}
function numberFieldSchema(field: EntityField) {
return z.any().superRefine((value, ctx) => {
if (value === undefined || value === null || value === '') return
if (typeof value !== 'number' || Number.isNaN(value)) {
addCustomIssue(ctx, 'INVALID_NUMBER_VALUE')
return
}
if (typeof field.min === 'number' && value < field.min)
addCustomIssue(ctx, 'FIELD_OUT_OF_RANGE')
if (typeof field.max === 'number' && value > field.max)
addCustomIssue(ctx, 'FIELD_OUT_OF_RANGE')
})
}
function dateFieldSchema() {
return z.any().superRefine((value, ctx) => {
Iif (value === undefined || value === null || value === '') return
if (!isValidDateString(value)) addCustomIssue(ctx, 'INVALID_DATE_FORMAT')
})
}
function selectFieldSchema(field: EntityField) {
const allowed = normalizeOptionValues(field.options)
return z.any().superRefine((value, ctx) => {
Iif (value === undefined || value === null || value === '') return
Iif (isDeletedPlaceholder(value)) return
if (typeof value !== 'string' || !allowed.includes(value)) {
addCustomIssue(ctx, 'INVALID_SELECT_VALUE')
}
})
}
function booleanFieldSchema() {
return z.any().superRefine((value, ctx) => {
Iif (value === undefined || value === null) return
Eif (typeof value !== 'boolean') addCustomIssue(ctx, 'INVALID_FIELD_VALUE')
})
}
// Deliberately scoped like the backend's buildItemDataSchema: number/date/select/
// boolean only — text/textarea/email length/maxLines is enforced live while typing
// (maxlength attrs) and by the backend; image/user/entity_ref/workflow keep their
// existing dedicated handling (ImageField.vue, RichReferenceSelect.vue, backend).
function buildItemDataSchema(fields: EntityField[]) {
const shape: Record<string, z.ZodTypeAny> = {}
for (const field of fields) {
Iif (!field.fieldId) continue
switch (field.type) {
case 'number':
shape[field.fieldId] = numberFieldSchema(field)
break
case 'date':
shape[field.fieldId] = dateFieldSchema()
break
case 'select':
shape[field.fieldId] = selectFieldSchema(field)
break
case 'boolean':
shape[field.fieldId] = booleanFieldSchema()
break
default:
break
}
}
return z.object(shape).passthrough()
}
// Validates every field (not just the first invalid one) so the form can
// surface all problems at once. Presence (validateMandatoryFields) and
// type/shape (Zod) are checked independently and merged — a field already
// flagged as missing isn't also flagged for its (absent) value's shape.
export function validateItemForm(
fields: EntityField[],
data: Record<string, unknown>
): FieldErrorMap {
const errors: FieldErrorMap = { ...validateMandatoryFields(fields, data) }
const result = buildItemDataSchema(fields).safeParse(data)
if (!result.success) {
for (const issue of result.error.issues) {
const fieldId = String(issue.path[0])
if (errors[fieldId]) continue
const code =
issue.code === 'custom' ? (issue.params as { code?: string } | undefined)?.code : undefined
errors[fieldId] = (code && ITEM_ERROR_CODE_TO_I18N_KEY[code]) || 'item.fieldInvalidValue'
}
}
return errors
}
|