(expr: SimpleExpr)
| 41 | }; |
| 42 | |
| 43 | export const validateSimpleExpr = (expr: SimpleExpr): boolean => { |
| 44 | const validateCondition = (condition: ConditionExpr): boolean => { |
| 45 | if (condition.args.length !== 2) { |
| 46 | // All condition expressions' args need to be [factor, value(s)] format. |
| 47 | return false; |
| 48 | } |
| 49 | if (isEqualityExpr(condition)) { |
| 50 | const [factor, value] = condition.args; |
| 51 | if (isStringFactor(factor)) return validateString(value); |
| 52 | if (isNumberFactor(factor)) return validateNumber(value); |
| 53 | if (isBooleanFactor(factor)) return typeof value === "boolean"; |
| 54 | } |
| 55 | if (isCompareExpr(condition)) { |
| 56 | const value = condition.args[1]; |
| 57 | return isNumber(value); |
| 58 | } |
| 59 | if (isCollectionExpr(condition)) { |
| 60 | const [factor, values] = condition.args; |
| 61 | if (isStringFactor(factor)) return validateStringArray(values); |
| 62 | if (isNumberFactor(factor)) return validateNumberArray(values); |
| 63 | } |
| 64 | if (isStringExpr(condition)) { |
| 65 | const value = condition.args[1]; |
| 66 | return validateString(value); |
| 67 | } |
| 68 | // unknown condition expr type |
| 69 | return false; |
| 70 | }; |
| 71 | const validateConditionGroup = (group: ConditionGroupExpr): boolean => { |
| 72 | const { args } = group; |
| 73 | if (args.length === 0) return false; |
| 74 | return args.every(validate); |
| 75 | }; |
| 76 | const validateRawString = (rawString: RawStringExpr): boolean => { |
| 77 | return Boolean(rawString.content); |
| 78 | }; |
| 79 | const validate = (expr: SimpleExpr): boolean => { |
| 80 | if (isConditionGroupExpr(expr)) return validateConditionGroup(expr); |
| 81 | if (isConditionExpr(expr)) return validateCondition(expr); |
| 82 | if (isRawStringExpr(expr)) return validateRawString(expr); |
| 83 | throw new Error(`unsupported expr '${JSON.stringify(expr)}'`); |
| 84 | }; |
| 85 | return validate(expr); |
| 86 | }; |
no test coverage detected