|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Validation Schemas for Simplified Filter Interface |
| 12 | + * |
| 13 | + * This module provides @kbn/config-schema validation schemas that correspond |
| 14 | + * to the TypeScript types defined in simplified_filter_types.ts. |
| 15 | + * |
| 16 | + * These schemas are used for runtime validation of API requests and responses |
| 17 | + * in * as Code APIs. |
| 18 | + */ |
| 19 | + |
| 20 | +import { schema } from '@kbn/config-schema'; |
| 21 | + |
| 22 | +// ==================================================================== |
| 23 | +// CORE FILTER OPERATOR AND VALUE SCHEMAS |
| 24 | +// ==================================================================== |
| 25 | + |
| 26 | +/** |
| 27 | + * Schema for supported filter operators |
| 28 | + */ |
| 29 | +export const filterOperatorSchema = schema.oneOf([ |
| 30 | + schema.literal('is'), |
| 31 | + schema.literal('is_not'), |
| 32 | + schema.literal('is_one_of'), |
| 33 | + schema.literal('is_not_one_of'), |
| 34 | + schema.literal('exists'), |
| 35 | + schema.literal('not_exists'), |
| 36 | + schema.literal('range'), |
| 37 | +]); |
| 38 | + |
| 39 | +/** |
| 40 | + * Schema for range values used in numeric and date filters |
| 41 | + */ |
| 42 | +export const rangeValueSchema = schema.object({ |
| 43 | + gte: schema.maybe(schema.oneOf([schema.number(), schema.string()])), |
| 44 | + lte: schema.maybe(schema.oneOf([schema.number(), schema.string()])), |
| 45 | + gt: schema.maybe(schema.oneOf([schema.number(), schema.string()])), |
| 46 | + lt: schema.maybe(schema.oneOf([schema.number(), schema.string()])), |
| 47 | +}); |
| 48 | + |
| 49 | +/** |
| 50 | + * Schema for all possible filter values |
| 51 | + * Supports single values, arrays, and range objects |
| 52 | + */ |
| 53 | +export const filterValueSchema = schema.oneOf([ |
| 54 | + schema.string(), |
| 55 | + schema.number(), |
| 56 | + schema.boolean(), |
| 57 | + schema.arrayOf(schema.oneOf([schema.string(), schema.number(), schema.boolean()])), |
| 58 | + rangeValueSchema, |
| 59 | +]); |
| 60 | + |
| 61 | +// ==================================================================== |
| 62 | +// BASE FILTER PROPERTIES (SHARED BY ALL SIMPLIFIED FILTERS) |
| 63 | +// ==================================================================== |
| 64 | + |
| 65 | +/** |
| 66 | + * Base properties shared by all simplified filters |
| 67 | + */ |
| 68 | +const baseFilterPropertiesSchema = { |
| 69 | + id: schema.maybe(schema.string()), |
| 70 | + pinned: schema.maybe(schema.boolean()), |
| 71 | + disabled: schema.maybe(schema.boolean()), |
| 72 | + controlledBy: schema.maybe(schema.string()), |
| 73 | + indexPattern: schema.maybe(schema.string()), |
| 74 | + negate: schema.maybe(schema.boolean()), |
| 75 | + label: schema.maybe(schema.string()), |
| 76 | +}; |
| 77 | + |
| 78 | +// ==================================================================== |
| 79 | +// SIMPLE FILTER CONDITION SCHEMAS |
| 80 | +// ==================================================================== |
| 81 | + |
| 82 | +/** |
| 83 | + * Base schema for simple filter conditions |
| 84 | + */ |
| 85 | +const baseFilterConditionSchema = { |
| 86 | + field: schema.string(), |
| 87 | +}; |
| 88 | + |
| 89 | +// ==================================================================== |
| 90 | +// DISCRIMINATED FILTER CONDITION SCHEMAS |
| 91 | +// ==================================================================== |
| 92 | + |
| 93 | +/** |
| 94 | + * Schema for filter conditions that require a value |
| 95 | + */ |
| 96 | +export const filterConditionWithValueSchema = schema.object({ |
| 97 | + ...baseFilterConditionSchema, |
| 98 | + operator: schema.oneOf([ |
| 99 | + schema.literal('is'), |
| 100 | + schema.literal('is_not'), |
| 101 | + schema.literal('is_one_of'), |
| 102 | + schema.literal('is_not_one_of'), |
| 103 | + schema.literal('range'), |
| 104 | + ]), |
| 105 | + value: filterValueSchema, |
| 106 | +}); |
| 107 | + |
| 108 | +/** |
| 109 | + * Schema for filter conditions that check existence only |
| 110 | + */ |
| 111 | +export const filterConditionExistsSchema = schema.object({ |
| 112 | + ...baseFilterConditionSchema, |
| 113 | + operator: schema.oneOf([schema.literal('exists'), schema.literal('not_exists')]), |
| 114 | + // value is intentionally omitted for exists/not_exists operators |
| 115 | +}); |
| 116 | + |
| 117 | +/** |
| 118 | + * Discriminated union schema for simple filter conditions |
| 119 | + */ |
| 120 | +export const simpleFilterConditionSchema = schema.oneOf([ |
| 121 | + filterConditionWithValueSchema, |
| 122 | + filterConditionExistsSchema, |
| 123 | +]); |
| 124 | + |
| 125 | +// ==================================================================== |
| 126 | +// FILTER GROUP SCHEMA (RECURSIVE) |
| 127 | +// ==================================================================== |
| 128 | + |
| 129 | +/** |
| 130 | + * Schema for logical filter groups with recursive structure |
| 131 | + * Uses lazy schema to handle recursive references |
| 132 | + * Note: Groups only contain logical structure (type, conditions) - no metadata properties |
| 133 | + */ |
| 134 | +export const filterGroupSchema = schema.object( |
| 135 | + { |
| 136 | + type: schema.oneOf([schema.literal('AND'), schema.literal('OR')]), |
| 137 | + conditions: schema.arrayOf( |
| 138 | + schema.oneOf([ |
| 139 | + simpleFilterConditionSchema, |
| 140 | + schema.lazy('filterGroup'), // Recursive reference |
| 141 | + ]) |
| 142 | + ), |
| 143 | + }, |
| 144 | + { meta: { id: 'filterGroup' } } |
| 145 | +); |
| 146 | + |
| 147 | +// ==================================================================== |
| 148 | +// RAW DSL FILTER SCHEMA |
| 149 | +// ==================================================================== |
| 150 | + |
| 151 | +/** |
| 152 | + * Schema for raw Elasticsearch Query DSL filters |
| 153 | + */ |
| 154 | +export const rawDSLFilterSchema = schema.object({ |
| 155 | + query: schema.recordOf(schema.string(), schema.any()), |
| 156 | +}); |
| 157 | + |
| 158 | +// ==================================================================== |
| 159 | +// SIMPLIFIED FILTER DISCRIMINATED UNION SCHEMA |
| 160 | +// ==================================================================== |
| 161 | + |
| 162 | +/** |
| 163 | + * Schema for simple condition filters (Tier 1) |
| 164 | + */ |
| 165 | +export const simplifiedConditionFilterSchema = schema.object({ |
| 166 | + ...baseFilterPropertiesSchema, |
| 167 | + condition: simpleFilterConditionSchema, |
| 168 | +}); |
| 169 | + |
| 170 | +/** |
| 171 | + * Schema for grouped condition filters (Tier 2-3) |
| 172 | + */ |
| 173 | +export const simplifiedGroupFilterSchema = schema.object({ |
| 174 | + ...baseFilterPropertiesSchema, |
| 175 | + group: filterGroupSchema, |
| 176 | +}); |
| 177 | + |
| 178 | +/** |
| 179 | + * Schema for raw DSL filters (Tier 4) |
| 180 | + */ |
| 181 | +export const simplifiedDSLFilterSchema = schema.object({ |
| 182 | + ...baseFilterPropertiesSchema, |
| 183 | + dsl: rawDSLFilterSchema, |
| 184 | +}); |
| 185 | + |
| 186 | +/** |
| 187 | + * Main discriminated union schema for SimplifiedFilter |
| 188 | + * Ensures exactly one of: condition, group, or dsl is present |
| 189 | + */ |
| 190 | +export const simplifiedFilterSchema = schema.oneOf([ |
| 191 | + simplifiedConditionFilterSchema, |
| 192 | + simplifiedGroupFilterSchema, |
| 193 | + simplifiedDSLFilterSchema, |
| 194 | +]); |
0 commit comments