@asteby/metacore-runtime-react 49.2.1 → 49.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/validator.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  // the same keys.
5
5
  import type { ActionFieldDef, FieldValidation } from './types'
6
6
  import type { FieldIssue } from './server-error'
7
+ import { applyOptionWhen, getDependsOn } from './option-when'
7
8
 
8
9
  const NIL_UUID = '00000000-0000-0000-0000-000000000000'
9
10
  const NUMERIC_TYPES = new Set(['int', 'integer', 'bigint', 'decimal', 'numeric', 'number', 'float', 'double'])
@@ -207,7 +208,13 @@ function checkBuiltin(slug: string, value: unknown): FieldIssue | undefined {
207
208
  }
208
209
  }
209
210
 
210
- function specFromField(field: ActionFieldDef): ValidationSpec {
211
+ /**
212
+ * `context` son los valores hermanos contra los que se evalúa el `when` de
213
+ * cada opción: una opción que su `when` descarta con el valor actual (forma de
214
+ * pago «99» con método PUE) es `invalid_option`, igual que una fuera de catálogo
215
+ * — y como la rechaza el kernel al guardar.
216
+ */
217
+ function specFromField(field: ActionFieldDef, context?: Record<string, unknown>): ValidationSpec {
211
218
  const v = fieldValidationOf(field)
212
219
  const spec: ValidationSpec = {
213
220
  required: !!field.required,
@@ -217,7 +224,10 @@ function specFromField(field: ActionFieldDef): ValidationSpec {
217
224
  max: v.max,
218
225
  custom: v.custom,
219
226
  }
220
- if (field.options?.length) spec.options = field.options.map(o => String(o.value))
227
+ if (field.options?.length) {
228
+ const applicable = context ? applyOptionWhen(field.options, context, getDependsOn(field)) : field.options
229
+ spec.options = applicable.map(o => String(o.value))
230
+ }
221
231
  return spec
222
232
  }
223
233
 
@@ -238,7 +248,7 @@ export function validateValues(
238
248
  values: Record<string, unknown>,
239
249
  ): Record<string, FieldIssue[]> {
240
250
  const bag: Record<string, FieldIssue[]> = {}
241
- walk(fields, values ?? {}, '', bag)
251
+ walk(fields, values ?? {}, '', bag, values ?? {})
242
252
  return bag
243
253
  }
244
254
 
@@ -247,6 +257,7 @@ function walk(
247
257
  values: Record<string, unknown>,
248
258
  prefix: string,
249
259
  bag: Record<string, FieldIssue[]>,
260
+ context: Record<string, unknown>,
250
261
  ): void {
251
262
  for (const field of fields) {
252
263
  const key = field.key
@@ -261,11 +272,12 @@ function walk(
261
272
  }
262
273
  rows.forEach((row, i) => {
263
274
  const obj = row && typeof row === 'object' ? (row as Record<string, unknown>) : {}
264
- walk(itemFieldsOf(field), obj, `${path}.${i}`, bag)
275
+ // Una celda puede depender de otra del renglón o del encabezado.
276
+ walk(itemFieldsOf(field), obj, `${path}.${i}`, bag, { ...context, ...obj })
265
277
  })
266
278
  continue
267
279
  }
268
- const issues = checkValue(raw, specFromField(field))
280
+ const issues = checkValue(raw, specFromField(field, context))
269
281
  if (issues.length) bag[path] = issues
270
282
  }
271
283
  }