UNPKG

kintone-as-code

Version:

A CLI tool for managing kintone applications as code with type-safe TypeScript schemas

62 lines 2.01 kB
import { toString } from './expression.js'; export class ComplexityError extends Error { depth; maxDepth; _tag = 'ComplexityError'; constructor(depth, maxDepth) { super(`Query depth ${depth} exceeds maximum ${maxDepth}`); this.depth = depth; this.maxDepth = maxDepth; } } export class LengthError extends Error { length; maxLength; _tag = 'LengthError'; constructor(length, maxLength) { super(`Query length ${length} exceeds maximum ${maxLength}`); this.length = length; this.maxLength = maxLength; } } const DEFAULT_MAX_DEPTH = 5; const DEFAULT_MAX_LENGTH = 10000; export const computeDepth = (expr) => { switch (expr._tag) { case 'condition': return 1; case 'not': return 1 + computeDepth(expr.expression); case 'and': case 'or': { const childDepths = expr.expressions.map(computeDepth); return 1 + (childDepths.length === 0 ? 0 : Math.max(...childDepths)); } } }; export const validateExpressionDepth = (expr, options) => { const maxDepth = options?.maxDepth ?? DEFAULT_MAX_DEPTH; const depth = computeDepth(expr); if (depth > maxDepth) { throw new ComplexityError(depth, maxDepth); } }; export const validateQueryStringLength = (query, options) => { const maxLength = options?.maxLength ?? DEFAULT_MAX_LENGTH; const length = query.length; if (length > maxLength) { throw new LengthError(length, maxLength); } }; export const validateExpression = (expr, options) => { const depthOpts = options?.maxDepth !== undefined ? { maxDepth: options.maxDepth } : undefined; validateExpressionDepth(expr, depthOpts); const query = toString(expr); const lengthOpts = options?.maxLength !== undefined ? { maxLength: options.maxLength } : undefined; validateQueryStringLength(query, lengthOpts); }; //# sourceMappingURL=validator.js.map