Validate a Filter Against a JSON Schema
Use @odata-filter/validation to ensure that the fields and operators in a parsed filter are consistent with your data model.
npm i @odata-filter/core @odata-filter/validation --save
import { tokenize, parse } from '@odata-filter/core';
import { validate } from '@odata-filter/validation';
const ast = parse(tokenize("age gte 21"));
validate(ast, {
age: { type: 'number' },
});
// No error thrown.
If the filter references an unknown field or uses an operator incompatible with the fieldโs type, validate throws a descriptive error.
const ast = parse(tokenize("age gte 'twenty one'"));
validate(ast, {
age: { type: 'number' },
});
// Error: Operator of type gte expects number_value, received string_value