|
1 | 1 | import assert from 'assert' |
2 | 2 | import { Ajv } from '@feathersjs/schema' |
3 | | -import { querySyntax, Type, Static, defaultAppConfiguration, getDataValidator, getValidator } from '../src' |
| 3 | +import { |
| 4 | + querySyntax, |
| 5 | + Type, |
| 6 | + Static, |
| 7 | + defaultAppConfiguration, |
| 8 | + getDataValidator, |
| 9 | + getValidator, |
| 10 | + queryProperties |
| 11 | +} from '../src' |
4 | 12 |
|
5 | 13 | describe('@feathersjs/schema/typebox', () => { |
6 | | - it('querySyntax', async () => { |
7 | | - const schema = Type.Object({ |
8 | | - name: Type.String(), |
9 | | - age: Type.Number() |
10 | | - }) |
11 | | - const querySchema = querySyntax(schema) |
| 14 | + describe('querySyntax', () => { |
| 15 | + it('basics', async () => { |
| 16 | + const schema = Type.Object({ |
| 17 | + name: Type.String(), |
| 18 | + age: Type.Number() |
| 19 | + }) |
| 20 | + const querySchema = querySyntax(schema) |
12 | 21 |
|
13 | | - type Query = Static<typeof querySchema> |
| 22 | + type Query = Static<typeof querySchema> |
14 | 23 |
|
15 | | - const query: Query = { |
16 | | - name: 'Dave', |
17 | | - age: { $gt: 42, $in: [50, 51] }, |
18 | | - $select: ['age', 'name'], |
19 | | - $sort: { |
20 | | - age: 1 |
| 24 | + const query: Query = { |
| 25 | + name: 'Dave', |
| 26 | + age: { $gt: 42, $in: [50, 51] }, |
| 27 | + $select: ['age', 'name'], |
| 28 | + $sort: { |
| 29 | + age: 1 |
| 30 | + } |
21 | 31 | } |
22 | | - } |
23 | 32 |
|
24 | | - const validator = new Ajv().compile(querySchema) |
25 | | - let validated = (await validator(query)) as any as Query |
| 33 | + const validator = new Ajv().compile(querySchema) |
| 34 | + let validated = (await validator(query)) as any as Query |
26 | 35 |
|
27 | | - assert.ok(validated) |
| 36 | + assert.ok(validated) |
| 37 | + |
| 38 | + validated = (await validator({ ...query, something: 'wrong' })) as any as Query |
| 39 | + assert.ok(!validated) |
| 40 | + }) |
28 | 41 |
|
29 | | - validated = (await validator({ ...query, something: 'wrong' })) as any as Query |
30 | | - assert.ok(!validated) |
| 42 | + it('queryProperties errors for unsupported query types', () => { |
| 43 | + assert.throws( |
| 44 | + () => |
| 45 | + queryProperties( |
| 46 | + Type.Object({ |
| 47 | + something: Type.Object({}) |
| 48 | + }) |
| 49 | + ), |
| 50 | + { |
| 51 | + message: |
| 52 | + "Can not create query syntax schema for property 'something'. Only types string, number, integer, boolean, null are allowed." |
| 53 | + } |
| 54 | + ) |
| 55 | + |
| 56 | + assert.throws( |
| 57 | + () => |
| 58 | + queryProperties( |
| 59 | + Type.Object({ |
| 60 | + otherThing: Type.Array(Type.String()) |
| 61 | + }) |
| 62 | + ), |
| 63 | + { |
| 64 | + message: |
| 65 | + "Can not create query syntax schema for property 'otherThing'. Only types string, number, integer, boolean, null are allowed." |
| 66 | + } |
| 67 | + ) |
| 68 | + }) |
| 69 | + |
| 70 | + it('querySyntax works with no properties', async () => { |
| 71 | + const schema = querySyntax(Type.Object({})) |
| 72 | + |
| 73 | + new Ajv().compile(schema) |
| 74 | + }) |
31 | 75 | }) |
32 | 76 |
|
33 | 77 | it('defaultAppConfiguration', async () => { |
|
0 commit comments