@@ -107,6 +107,7 @@ React Query Builder is designed to be highly configurable. You can control:
107107- field definitions
108108- initial and controlled query data
109109- drag and drop support
110+ - validation rules
110111- read-only behavior
111112- supported group types
112113- theming
@@ -145,6 +146,7 @@ Each field can define:
145146- ` type ` : field type
146147- ` operators ` : allowed operators for the field
147148- ` value ` : source value for ` LIST ` , ` MULTI_LIST ` , or ` STATEMENT `
149+ - ` validation ` : optional validation rules for the field
148150
149151#### Type
150152
@@ -195,6 +197,55 @@ Prefer `IN`, `CONTAINS`, and `NOT_CONTAINS`.
195197
196198` IS_NULL ` and ` IS_NOT_NULL ` are value-less operators, so the built-in UI does not render a value input for them.
197199
200+ #### Validation
201+
202+ Validation is defined per field and is type-aware.
203+
204+ ``` typescript
205+ const fields: IBuilderFieldProps [] = [
206+ {
207+ field: ' NAME' ,
208+ label: ' Name' ,
209+ type: ' TEXT' ,
210+ operators: [' EQUAL' , ' CONTAINS' ],
211+ validation: {
212+ required: true ,
213+ minLength: 2 ,
214+ maxLength: 50 ,
215+ },
216+ },
217+ {
218+ field: ' PRICE' ,
219+ label: ' Price' ,
220+ type: ' NUMBER' ,
221+ operators: [' EQUAL' , ' BETWEEN' ],
222+ validation: {
223+ min: 0 ,
224+ range: {
225+ requireAscending: true ,
226+ allowEqual: false ,
227+ },
228+ },
229+ },
230+ ];
231+ ```
232+
233+ Built-in validation supports:
234+
235+ - shared rules like ` required ` , ` oneOf ` , and ` custom `
236+ - text rules like ` minLength ` , ` maxLength ` , and ` matches `
237+ - number rules like ` min ` , ` max ` , ` integer ` , ` positive ` , and ` negative `
238+ - date rules like ` minDate ` and ` maxDate `
239+ - multi-list rules like ` minItems ` and ` maxItems `
240+ - range validation for ` BETWEEN ` and ` NOT_BETWEEN `
241+
242+ If you use ` range ` , you can validate both the range shape and the relationship between values:
243+
244+ - ` requireAscending `
245+ - ` allowEqual `
246+ - ` validate `
247+ - ` message `
248+
198249### Data
199250
200251` data ` is a controlled prop. Pass an array of rules and groups, and update it through ` onChange ` .
@@ -255,6 +306,9 @@ Important `Builder` props:
255306- ` draggable?: boolean `
256307- ` singleRootGroup?: boolean `
257308- ` groupTypes?: 'with-modifiers' | 'without-modifiers' | 'both' `
309+ - ` validator?: IBuilderValidator `
310+ - ` onStateChange?: (state: IBuilderStateChange) => void `
311+ - ` showValidation?: boolean `
258312
259313#### ` readOnly `
260314
@@ -297,6 +351,37 @@ Controls what kinds of groups users can add:
297351
298352When set to ` 'both' ` , the ` Add Group ` action becomes a popover that lets the user choose which group type to create.
299353
354+ #### ` validator `
355+
356+ ` validator ` lets you override the built-in validation engine with your own implementation.
357+
358+ The validator receives:
359+
360+ - the current query data
361+ - builder validation context including ` fields ` , ` singleRootGroup ` , and ` groupTypes `
362+
363+ It should return an ` IBuilderValidationResult ` or a promise resolving to one.
364+
365+ This makes it possible to plug in optional adapters such as Yup or Joi without coupling the core builder to a specific validation library.
366+
367+ #### ` onStateChange `
368+
369+ ` onStateChange ` emits the current builder state together with validation output:
370+
371+ ``` typescript
372+ {
373+ data : DenormalizedQuery ;
374+ isValid : boolean ;
375+ validation : IBuilderValidationResult ;
376+ }
377+ ```
378+
379+ ` onChange ` still emits only the denormalized query data.
380+
381+ #### ` showValidation `
382+
383+ When ` showValidation ` is ` true ` , the built-in ` Rule ` component renders validation issues under the affected rule.
384+
300385### Components
301386
302387You can fully customize the rendered components through the ` components ` prop.
@@ -420,3 +505,19 @@ const strings: IStrings = {
420505` ANY_IN ` , ` LIKE ` , and ` NOT_LIKE ` are deprecated here as well and will be removed in ` 2.0.0 ` .
421506
422507It is not required to translate every string. Any string you omit falls back to the built-in defaults.
508+
509+ Validation copy is also configurable through ` strings.validation ` , for example:
510+
511+ ``` typescript
512+ const strings: IStrings = {
513+ validation: {
514+ required: ' This field is required' ,
515+ min: ' Value must be at least {min}' ,
516+ max: ' Value must be at most {max}' ,
517+ minItems: ' Select at least {min} values' ,
518+ maxItems: ' Select at most {max} values' ,
519+ },
520+ };
521+ ```
522+
523+ Validation strings support placeholder replacement for values such as ` {field} ` , ` {operator} ` , ` {min} ` , and ` {max} ` .
0 commit comments