Skip to content

Commit cf3d70e

Browse files
committed
Add documentation on parseNumbers and debugMode
1 parent b3f0c88 commit cf3d70e

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

docs/api/export.mdx

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ function formatQuery(
2424
- MongoDB
2525
- Common Expression Language (CEL)
2626

27-
:::info
28-
29-
The inversion operator (setting `not: true` for a rule group) is currently unsupported for the MongoDB format, but rules can be created using the `"!="` operator.
30-
31-
:::
32-
3327
For the next few sections, assume the `query` variable has been defined as:
3428

3529
```ts
@@ -169,6 +163,12 @@ Output:
169163
`{"$and":[{"firstName":{"$eq":"Steve"}},{"lastName":{"$eq":"Vai"}}]}`;
170164
```
171165

166+
:::info
167+
168+
For now, the MongoDB export format has two major shortcomings. For one, it does not support the inversion operator (setting `not: true` for a rule group), however rules _can_ be created using the `"!="` operator. Also, if the query uses [independent combinators](./querybuilder#independentcombinators), it will not be processed and `formatQuery(query, 'mongodb')` will always return the [fallback expression](#fallback-expression).
169+
170+
:::
171+
172172
### Common Expression Language
173173

174174
For Common Expression Language (CEL) output, use the "cel" format.
@@ -185,7 +185,65 @@ Output:
185185

186186
## Configuration
187187

188-
An object can be passed as the second argument instead of a string in order to have more detailed control over the output.
188+
An object can be passed as the second argument instead of a string to have more fine-grained control over the output.
189+
190+
### Parse numbers
191+
192+
Since HTML `<input>` controls store values as strings (even when `type="number"`), exporting a query to various formats may produce a string representation of a value when a true numeric value is required or more appropriate. Set the `parseNumbers` option to `true` and `formatQuery` will attempt to convert all values to numbers, falling back to the original value if `parseFloat(value)` returns `NaN` (not a number).
193+
194+
```ts
195+
const query: RuleGroupType = {
196+
combinator: 'and',
197+
not: false,
198+
rules: [
199+
{
200+
field: 'digits',
201+
operator: '=',
202+
value: '20',
203+
},
204+
{
205+
field: 'age',
206+
operator: 'between',
207+
value: '26, 52',
208+
},
209+
{
210+
field: 'lastName',
211+
operator: '=',
212+
value: 'Vai',
213+
},
214+
],
215+
};
216+
217+
// Default configuration - all values are strings:
218+
formatQuery(query, { format: 'sql' });
219+
// Returns: "(digits = '20' and age between '26' and '52' and lastName = 'Vai')"
220+
221+
// `parseNumbers: true` - numeric strings converted to actual numbers:
222+
formatQuery(query, { format: 'sql', parseNumbers: true });
223+
// Returns: "(digits = 20 and age between 26 and 52 and lastName = 'Vai')"
224+
```
225+
226+
:::info
227+
228+
To avoid information loss, this option is more strict about what qualifies as "numeric" than [the standard `parseFloat` function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat). Put simply, `parseFloat` works with any string that _starts_ with a numeric sequence, ignoring the rest of the string beginning with the first non-numeric character. When `parseNumbers` is `true`, `formatQuery` will only convert a `value` to a `number` if it appears to be numeric _in its entirety_ (after trimming whitespace).
229+
230+
The following expressions all evaluate to `true`:
231+
232+
```ts
233+
parseFloat('000111abcdef') === 111;
234+
235+
formatQuery(
236+
{ rules: [{ field: 'f', operator: '=', value: '000111abcdef' }] },
237+
{ format: 'sql', parseNumbers: true }
238+
) === "(f = '000111abcdef')";
239+
240+
formatQuery(
241+
{ rules: [{ field: 'f', operator: '=', value: ' 000111 ' }] },
242+
{ format: 'sql', parseNumbers: true }
243+
) === '(f = 111)';
244+
```
245+
246+
:::
189247

190248
### Value processor
191249

@@ -198,13 +256,13 @@ const query: RuleGroupType = {
198256
rules: [
199257
{
200258
field: 'instrument',
201-
value: ['Guitar', 'Vocals'],
202259
operator: 'in',
260+
value: ['Guitar', 'Vocals'],
203261
},
204262
{
205263
field: 'lastName',
206-
value: 'Vai',
207264
operator: '=',
265+
value: 'Vai',
208266
},
209267
],
210268
};
@@ -218,7 +276,7 @@ const valueProcessor = (field, operator, value) => {
218276
};
219277

220278
formatQuery(query, { format: 'sql', valueProcessor });
221-
// Output: "(instrument in ('Guitar','Vocals') and lastName = 'Vai')"
279+
// Returns: "(instrument in ('Guitar','Vocals') and lastName = 'Vai')"
222280
```
223281

224282
### Quote field names
@@ -227,7 +285,7 @@ Some database engines wrap field names in backticks. This can be configured with
227285

228286
```ts
229287
formatQuery(query, { format: 'sql', quoteFieldNamesWith: '`' });
230-
// Output: "(`firstName` = 'Steve' and `lastName` = 'Vai')"
288+
// Returns: "(`firstName` = 'Steve' and `lastName` = 'Vai')"
231289
```
232290

233291
### Parameter prefix
@@ -306,21 +364,21 @@ formatQuery(query, {
306364
format: 'sql',
307365
validator: () => false,
308366
});
309-
// Output: "(1 = 1)" <-- see `fallbackExpression` option
367+
// Returns: "(1 = 1)" <-- see `fallbackExpression` option
310368

311369
// Rule "r1" is invalid based on the validation map
312370
formatQuery(query, {
313371
format: 'sql',
314372
validator: () => ({ r1: false }),
315373
});
316-
// Output: "(lastName = 'Vai')" <-- skipped `firstName` rule with `id === 'r1'`
374+
// Returns: "(lastName = 'Vai')" <-- skipped `firstName` rule with `id === 'r1'`
317375

318376
// Rule "r1" is invalid based on the field validator for `firstName`
319377
formatQuery(query, {
320378
format: 'sql',
321379
fields: [{ name: 'firstName', validator: () => false }],
322380
});
323-
// Output: "(lastName = 'Vai')" <-- skipped `firstName` rule because field validator returned `false`
381+
// Returns: "(lastName = 'Vai')" <-- skipped `firstName` rule because field validator returned `false`
324382
```
325383

326384
### Automatic validation

docs/api/querybuilder.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,12 @@ Pass `true` to display a drag handle to the left of each group header and rule.
735735

736736
Pass `true` to disable all subcomponents and prevent changes to the query. Pass an array of paths to disable specific rules and/or groups, e.g. `disabled={[[0]]}` will disable the first rule and its subcomponents but nothing else.
737737

738+
### `debugMode`
739+
740+
`boolean` (default `false`) [_Click for demo_](https://react-querybuilder.github.io/react-querybuilder/#debugMode=true)
741+
742+
Pass `true` to log debugging information to the console.
743+
738744
### `validator`
739745

740746
`QueryValidator` [_Click for demo_](https://react-querybuilder.github.io/react-querybuilder/#validateQuery=true)

0 commit comments

Comments
 (0)