Skip to content

Commit

Permalink
Allow filter:[not ]between strings in Knex
Browse files Browse the repository at this point in the history
Fixes #19.
  • Loading branch information
jstayton committed Jul 29, 2020
1 parent 22818ad commit 514ea7e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ That said, the default Knex adapter supports the following operators/values:
- `not like`: string
- `ilike`: string
- `not ilike`: string
- `between`: array of two numbers
- `not between`: array of two numbers
- `between`: array of two strings and/or numbers
- `not between`: array of two strings and/or numbers

### Defining the Schema

Expand Down
10 changes: 8 additions & 2 deletions src/adapters/knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ class KnexAdapter extends BaseAdapter {
'filter:not like': schema.string(),
'filter:ilike': schema.string(),
'filter:not ilike': schema.string(),
'filter:between': schema.array().length(2).items(schema.number()),
'filter:not between': schema.array().length(2).items(schema.number()),
'filter:between': schema
.array()
.length(2)
.items(schema.number(), schema.string()),
'filter:not between': schema
.array()
.length(2)
.items(schema.number(), schema.string()),
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/src/adapters/knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,14 @@ describe('validation', () => {
).toBe(true)
})

test('permits an array of two string values', () => {
const validator = new KnexAdapter().validator

expect(
validator.validateValue('filter:between', 'test', ['test', 'test'])
).toBe(true)
})

test('throws for a non-permitted value', () => {
const validator = new KnexAdapter().validator

Expand All @@ -505,6 +513,14 @@ describe('validation', () => {
).toBe(true)
})

test('permits an array of two string values', () => {
const validator = new KnexAdapter().validator

expect(
validator.validateValue('filter:not between', 'test', ['test', 'test'])
).toBe(true)
})

test('throws for a non-permitted value', () => {
const validator = new KnexAdapter().validator

Expand Down

0 comments on commit 514ea7e

Please sign in to comment.