|
| 1 | +import { |
| 2 | + V2Condition, |
| 3 | + V2ConditionOperator, |
| 4 | + JSONFormsRule, |
| 5 | + JSONFormsSchemaBasedCondition, |
| 6 | +} from '../common/types'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Builds a JSON Schema for the CONTAINS operator. |
| 10 | + * Matches strings that contain the specified value as a substring. |
| 11 | + */ |
| 12 | +export const buildContainsSchema = (value: string): Record<string, unknown> => { |
| 13 | + return { |
| 14 | + pattern: value, |
| 15 | + type: 'string', |
| 16 | + }; |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * Builds a JSON Schema for the DOES_NOT_HAVE_INPUT operator. |
| 21 | + */ |
| 22 | +export const buildDoesNotHaveInputSchema = (): Record<string, unknown> => { |
| 23 | + return { |
| 24 | + not: buildHasInputSchema(), |
| 25 | + }; |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * Builds a JSON Schema for the HAS_INPUT operator. |
| 30 | + * Matches when the field has meaningful input (non-null, non-empty). |
| 31 | + */ |
| 32 | +export const buildHasInputSchema = (): Record<string, unknown> => { |
| 33 | + return { |
| 34 | + allOf: [ |
| 35 | + { not: { type: 'null' } }, |
| 36 | + { |
| 37 | + anyOf: [ |
| 38 | + { type: 'array', minItems: 1 }, |
| 39 | + { type: 'boolean' }, |
| 40 | + { type: 'number' }, |
| 41 | + { type: 'object', minProperties: 1 }, |
| 42 | + { type: 'string', minLength: 1 }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + ], |
| 46 | + }; |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * Builds a JSON Schema for the INPUT_IS_EXACTLY operator. |
| 51 | + * Matches when the field value equals the specified value exactly. |
| 52 | + * Uses `enum` for simple matching without type constraints. |
| 53 | + */ |
| 54 | +export const buildInputIsExactlySchema = ( |
| 55 | + value: string | number | boolean | null, |
| 56 | +): Record<string, unknown> => { |
| 57 | + if (value === null) { |
| 58 | + return { type: 'null' }; |
| 59 | + } |
| 60 | + if (typeof value === 'boolean') { |
| 61 | + return { const: value }; |
| 62 | + } |
| 63 | + if (typeof value === 'number') { |
| 64 | + // Allow both number and string representation |
| 65 | + return { enum: [value, String(value)] }; |
| 66 | + } |
| 67 | + // String value - also allow numeric match if the string is a valid number |
| 68 | + const numValue = Number(value); |
| 69 | + if (!isNaN(numValue) && value !== '') { |
| 70 | + return { enum: [numValue, value] }; |
| 71 | + } |
| 72 | + return { const: value }; |
| 73 | +}; |
| 74 | + |
| 75 | +const VALID_OPERATORS: V2ConditionOperator[] = [ |
| 76 | + 'CONTAINS', |
| 77 | + 'DOES_NOT_HAVE_INPUT', |
| 78 | + 'HAS_INPUT', |
| 79 | + 'INPUT_IS_EXACTLY', |
| 80 | +]; |
| 81 | + |
| 82 | +/** |
| 83 | + * Gets the operator schema for a condition (without field wrapper). |
| 84 | + * Used internally by buildConditionSchema and buildSchemaBasedCondition. |
| 85 | + */ |
| 86 | +export const getOperatorSchema = ( |
| 87 | + condition: V2Condition, |
| 88 | +): Record<string, unknown> => { |
| 89 | + switch (condition.operator) { |
| 90 | + case 'CONTAINS': |
| 91 | + if (condition.value === undefined || condition.value === null) { |
| 92 | + throw new Error( |
| 93 | + `CONTAINS operator requires a value for condition '${condition.id}'`, |
| 94 | + ); |
| 95 | + } |
| 96 | + return buildContainsSchema(String(condition.value)); |
| 97 | + |
| 98 | + case 'DOES_NOT_HAVE_INPUT': |
| 99 | + return buildDoesNotHaveInputSchema(); |
| 100 | + |
| 101 | + case 'HAS_INPUT': |
| 102 | + return buildHasInputSchema(); |
| 103 | + |
| 104 | + case 'INPUT_IS_EXACTLY': |
| 105 | + if (condition.value === undefined) { |
| 106 | + throw new Error( |
| 107 | + `INPUT_IS_EXACTLY operator requires a value for condition '${condition.id}'`, |
| 108 | + ); |
| 109 | + } |
| 110 | + return buildInputIsExactlySchema(condition.value); |
| 111 | + |
| 112 | + default: |
| 113 | + throw new Error( |
| 114 | + `Unknown operator '${condition.operator}' in condition '${condition.id}'`, |
| 115 | + ); |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +/** |
| 120 | + * Builds a JSON Schema for a single condition based on its operator. |
| 121 | + * Wraps the operator schema in a properties structure for root scope validation. |
| 122 | + */ |
| 123 | +export const buildConditionSchema = ( |
| 124 | + condition: V2Condition, |
| 125 | +): Record<string, unknown> => { |
| 126 | + const operatorSchema = getOperatorSchema(condition); |
| 127 | + |
| 128 | + // Wrap in properties structure for the field (used with scope: "#") |
| 129 | + return { |
| 130 | + properties: { |
| 131 | + [condition.field]: operatorSchema, |
| 132 | + }, |
| 133 | + required: [condition.field], |
| 134 | + }; |
| 135 | +}; |
| 136 | + |
| 137 | +/** |
| 138 | + * Builds a JSONForms schema-based condition from one or more V2 conditions. |
| 139 | + * |
| 140 | + * For single conditions: Uses field-specific scope (e.g., "#/properties/fieldName") |
| 141 | + * which is the standard JSONForms pattern and most reliable across implementations. |
| 142 | + * |
| 143 | + * For multiple conditions: Uses root scope "#" with allOf combining property schemas. |
| 144 | + */ |
| 145 | +export const buildSchemaBasedCondition = ( |
| 146 | + conditions: V2Condition[], |
| 147 | +): JSONFormsSchemaBasedCondition => { |
| 148 | + if (conditions.length === 0) { |
| 149 | + throw new Error('At least one condition is required'); |
| 150 | + } |
| 151 | + |
| 152 | + if (conditions.length === 1) { |
| 153 | + // Single condition: use field-specific scope for better compatibility |
| 154 | + const condition = conditions[0]; |
| 155 | + return { |
| 156 | + scope: `#/properties/${condition.field}`, |
| 157 | + schema: getOperatorSchema(condition), |
| 158 | + }; |
| 159 | + } |
| 160 | + |
| 161 | + // Multiple conditions: use root scope with allOf |
| 162 | + const allOfSchemas = conditions.map((condition) => |
| 163 | + buildConditionSchema(condition), |
| 164 | + ); |
| 165 | + |
| 166 | + return { |
| 167 | + scope: '#', |
| 168 | + schema: { |
| 169 | + allOf: allOfSchemas, |
| 170 | + }, |
| 171 | + }; |
| 172 | +}; |
| 173 | + |
| 174 | +/** |
| 175 | + * Creates a complete JSONForms rule for a section with the given conditions. |
| 176 | + * Uses SHOW effect by default. |
| 177 | + */ |
| 178 | +export const createSectionRule = (conditions: V2Condition[]): JSONFormsRule => { |
| 179 | + return { |
| 180 | + effect: 'SHOW', |
| 181 | + condition: buildSchemaBasedCondition(conditions), |
| 182 | + }; |
| 183 | +}; |
| 184 | + |
| 185 | +/** |
| 186 | + * Validates conditions for a section. |
| 187 | + * Throws an error if any condition is invalid. |
| 188 | + */ |
| 189 | +export const validateConditions = ( |
| 190 | + conditions: V2Condition[], |
| 191 | + fieldNames: string[], |
| 192 | +): void => { |
| 193 | + for (const condition of conditions) { |
| 194 | + // Validate operator |
| 195 | + if (!VALID_OPERATORS.includes(condition.operator)) { |
| 196 | + throw new Error(`Invalid operator '${condition.operator}'`); |
| 197 | + } |
| 198 | + |
| 199 | + // Validate field exists |
| 200 | + if (!fieldNames.includes(condition.field)) { |
| 201 | + throw new Error(`Unknown field '${condition.field}'`); |
| 202 | + } |
| 203 | + |
| 204 | + // Validate required values |
| 205 | + if ( |
| 206 | + condition.operator === 'CONTAINS' && |
| 207 | + (condition.value === undefined || condition.value === null) |
| 208 | + ) { |
| 209 | + throw new Error(`CONTAINS operator requires a value`); |
| 210 | + } |
| 211 | + |
| 212 | + if ( |
| 213 | + condition.operator === 'INPUT_IS_EXACTLY' && |
| 214 | + condition.value === undefined |
| 215 | + ) { |
| 216 | + throw new Error(`INPUT_IS_EXACTLY operator requires a value`); |
| 217 | + } |
| 218 | + } |
| 219 | +}; |
0 commit comments