Skip to content

Commit

Permalink
Allow <, <=, >=, > filter strings in Knex for date
Browse files Browse the repository at this point in the history
  • Loading branch information
jstayton committed Oct 22, 2020
1 parent ec7e850 commit 9776f85
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
8 changes: 4 additions & 4 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ That said, the default Knex adapter supports the following operators/values:
- `=`: string, number, or boolean
- `!=`: string, number, or boolean
- `<>`: string, number, or boolean
- `>`: number
- `>=`: number
- `<`: number
- `<=`: number
- `>`: string or number
- `>=`: string or number
- `<`: string or number
- `<=`: string or number
- `is`: `null`
- `is not`: `null`
- `in`: array of strings and/or numbers
Expand Down
8 changes: 4 additions & 4 deletions src/adapters/knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class KnexAdapter extends BaseAdapter {
'filter:<>': schema
.alternatives()
.try(schema.boolean(), schema.number(), schema.string()),
'filter:>': schema.number(),
'filter:>=': schema.number(),
'filter:<': schema.number(),
'filter:<=': schema.number(),
'filter:>': schema.alternatives().try(schema.number(), schema.string()),
'filter:>=': schema.alternatives().try(schema.number(), schema.string()),
'filter:<': schema.alternatives().try(schema.number(), schema.string()),
'filter:<=': schema.alternatives().try(schema.number(), schema.string()),
'filter:is': schema.any().valid(null),
'filter:is not': schema.any().valid(null),
'filter:in': schema.array().items(schema.number(), schema.string()),
Expand Down
48 changes: 36 additions & 12 deletions test/src/adapters/knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,18 @@ describe('validation', () => {
expect(validator.validateValue('filter:>', 'test', 123)).toBe(true)
})

test('permits a string value', () => {
const validator = new KnexAdapter().validator

expect(validator.validateValue('filter:>', 'test', 'valid')).toBe(true)
})

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

expect(() =>
validator.validateValue('filter:>', 'test', 'invalid')
).toThrow(new ValidationError('test must be a number'))
expect(() => validator.validateValue('filter:>', 'test', false)).toThrow(
new ValidationError('test must be one of [number, string]')
)
})
})

Expand All @@ -299,12 +305,18 @@ describe('validation', () => {
expect(validator.validateValue('filter:>=', 'test', 123)).toBe(true)
})

test('permits a string value', () => {
const validator = new KnexAdapter().validator

expect(validator.validateValue('filter:>=', 'test', 'valid')).toBe(true)
})

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

expect(() =>
validator.validateValue('filter:>=', 'test', 'invalid')
).toThrow(new ValidationError('test must be a number'))
expect(() => validator.validateValue('filter:>=', 'test', false)).toThrow(
new ValidationError('test must be one of [number, string]')
)
})
})

Expand All @@ -315,12 +327,18 @@ describe('validation', () => {
expect(validator.validateValue('filter:<', 'test', 123)).toBe(true)
})

test('permits a string value', () => {
const validator = new KnexAdapter().validator

expect(validator.validateValue('filter:<', 'test', 'valid')).toBe(true)
})

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

expect(() =>
validator.validateValue('filter:<', 'test', 'invalid')
).toThrow(new ValidationError('test must be a number'))
expect(() => validator.validateValue('filter:<', 'test', false)).toThrow(
new ValidationError('test must be one of [number, string]')
)
})
})

Expand All @@ -331,12 +349,18 @@ describe('validation', () => {
expect(validator.validateValue('filter:<=', 'test', 123)).toBe(true)
})

test('permits a string value', () => {
const validator = new KnexAdapter().validator

expect(validator.validateValue('filter:<=', 'test', 'valid')).toBe(true)
})

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

expect(() =>
validator.validateValue('filter:<=', 'test', 'invalid')
).toThrow(new ValidationError('test must be a number'))
expect(() => validator.validateValue('filter:<=', 'test', false)).toThrow(
new ValidationError('test must be one of [number, string]')
)
})
})

Expand Down

0 comments on commit 9776f85

Please sign in to comment.