Skip to content

Commit

Permalink
docs(field_expressions): add caveat around multiple field_expressions…
Browse files Browse the repository at this point in the history
… to docs, #239
  • Loading branch information
MrSwitch committed Aug 2, 2022
1 parent 2bd8a84 commit a27a428
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1236,3 +1236,21 @@ await dare.patch({
},
});
```
# Caveats
## Only one column name may appear in a Field Expression
Dare can't have more than two model field/column names in the same field expression.

e.g. Fields array....
```js
[{
// Can't have both fist_name and last_name in the same field expression string
`displayName`: 'CONCAT(first_name, last_name)`
}]
```
To work around this we'd simply use post-formatting, you can do this yourself or create a Generated Field (handler). To have Dare do it.
34 changes: 27 additions & 7 deletions test/specs/unwrap_field.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import DareError from '../../src/utils/error.js';

describe('utils/unwrap_field', () => {

// Should unwrap SQL Formating to underlying column name
/*
* Supported Field Expressions
* Should unwrap SQL Formating to underlying column name
*/
[
'field',
'DATE(field)',
Expand Down Expand Up @@ -55,22 +58,39 @@ describe('utils/unwrap_field', () => {

});

// Should unwrap SQL Formating to underlying column name

/*
* Unsupported field expressions
* Shall throw an error when unwrapping SQL these field expressions.
* Either the syntax errors or unsupported SQL in Dare.
*/
[
// Bad Syntax
'field(',
'CONCAT(field, secret)',
'IF(field < field2, "yes", "no")',
'IF(field IS NOT NULL, "yes", "no")',
'IF(field < "string"str, "yes", "no")',
'IF(field = \'string", "yes", "no")',
'DATE_FORMAT(field, ',
'IF(field <<< 123, "yes", "no")',

/*
* VALID SYNTAX, BUT UNSUPPORTED
*/

// IS NOT NULL
'IF(field IS NOT NULL, "yes", "no")',

// Bad spacing
'IF(field<123, "yes", "no")',
'DATE_FORMAT(field, '

// More than 1 field requested
'CONCAT(field, secret)',
'IF(field < field2, "yes", "no")'

].forEach(test => {

it(`errors: ${JSON.stringify(test)}`, () => {

// Expect the formatted list of fields to be identical to the inputted value
// Expect the field expression unwrapping to throw a Dare Error
expect(unwrap_field.bind(null, test)).to.throw(DareError);

});
Expand Down

0 comments on commit a27a428

Please sign in to comment.