From e304bebf19340b825df10a17315b534f5dca0219 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 23 Sep 2023 22:15:06 -0300 Subject: data: WIP input validation Static only, as of this commit. --- src/data/things/composite.js | 55 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/data/things/composite.js b/src/data/things/composite.js index 791b836..27b345c 100644 --- a/src/data/things/composite.js +++ b/src/data/things/composite.js @@ -548,7 +548,12 @@ export function templateCompositeFrom(description) { }); const wrongTypeInputNames = []; - const wrongInputCallInputNames = []; + + const expectedStaticValueInputNames = []; + const expectedStaticDependencyInputNames = []; + + const validateFailedInputNames = []; + const validateFailedErrors = []; for (const [name, value] of Object.entries(inputOptions)) { if (misplacedInputNames.includes(name)) { @@ -559,6 +564,37 @@ export function templateCompositeFrom(description) { wrongTypeInputNames.push(name); continue; } + + const descriptionShape = getInputTokenShape(description.inputs[name]); + const descriptionValue = getInputTokenValue(description.inputs[name]); + + const tokenShape = (isInputToken(value) ? getInputTokenShape(value) : null); + const tokenValue = (isInputToken(value) ? getInputTokenValue(value) : null); + + if (descriptionShape === 'input.staticValue') { + if (tokenShape !== 'input.value') { + expectedStaticValueInputNames.push(name); + continue; + } + } + + if (descriptionShape === 'input.staticDependency') { + if (typeof value !== 'string' && tokenShape !== 'input.dependency') { + expectedStaticDependencyInputNames.push(name); + continue; + } + } + + if (descriptionValue && 'validate' in descriptionValue) { + if (tokenShape === 'input.value') { + try { + descriptionValue.validate(tokenValue); + } catch (error) { + validateFailedInputNames.push(name); + validateFailedErrors.push(error); + } + } + } } if (!empty(misplacedInputNames)) { @@ -569,6 +605,23 @@ export function templateCompositeFrom(description) { inputOptionsAggregate.push(new Error(`Required these inputs: ${missingInputNames.join(', ')}`)); } + if (!empty(expectedStaticDependencyInputNames)) { + inputOptionsAggregate.push(new Error(`Expected static dependencies: ${expectedStaticDependencyInputNames.join(', ')}`)); + } + + if (!empty(expectedStaticValueInputNames)) { + inputOptionsAggregate.push(new Error(`Expected static values: ${expectedStaticValueInputNames.join(', ')}`)); + } + + for (const {name, validationError} of stitchArrays({ + name: validateFailedInputNames, + validationError: validateFailedErrors, + })) { + const error = new Error(`${name}: Validation failed for static value`); + error.cause = validationError; + inputOptionsAggregate.push(error); + } + for (const name of wrongTypeInputNames) { const type = typeof inputOptions[name]; inputOptionsAggregate.push(new Error(`${name}: Expected string or input() call, got ${type}`)); -- cgit 1.3.0-6-gf8a5