-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
filter-json-schema.unit.ts
193 lines (167 loc) · 5.41 KB
/
filter-json-schema.unit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/repository-json-schema
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {Entity, Filter, hasMany, model, property} from '@loopback/repository';
import {expect} from '@loopback/testlab';
import * as Ajv from 'ajv';
import {JsonSchema} from '../..';
import {
getFilterJsonSchemaFor,
getWhereJsonSchemaFor,
} from '../../filter-json-schema';
describe('getFilterJsonSchemaFor', () => {
let ajv: Ajv.Ajv;
let customerFilterSchema: JsonSchema;
let orderFilterSchema: JsonSchema;
beforeEach(() => {
ajv = new Ajv();
customerFilterSchema = getFilterJsonSchemaFor(Customer);
orderFilterSchema = getFilterJsonSchemaFor(Order);
});
it('produces a valid schema', () => {
const isValid = ajv.validateSchema(customerFilterSchema);
const SUCCESS_MSG = 'Filter schema is a valid JSON Schema';
const result = isValid ? SUCCESS_MSG : ajv.errorsText(ajv.errors!);
expect(result).to.equal(SUCCESS_MSG);
});
it('allows an empty filter', () => {
expectSchemaToAllowFilter(customerFilterSchema, {});
});
it('allows all top-level filter properties', () => {
const filter: Required<Filter> = {
where: {id: 1},
fields: {id: true, name: true},
include: [{relation: 'orders'}],
offset: 0,
limit: 10,
order: ['id DESC'],
skip: 0,
};
expectSchemaToAllowFilter(customerFilterSchema, filter);
});
it('describes "where" as an object', () => {
const filter = {where: 'invalid-where'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
keyword: 'type',
dataPath: '.where',
message: 'should be object',
},
]);
});
it('describes "fields" as an object', () => {
const filter = {fields: 'invalid-fields'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
keyword: 'type',
dataPath: '.fields',
message: 'should be object',
},
]);
});
it('describes "include" as an array for models with relations', () => {
const filter = {include: 'invalid-include'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
keyword: 'type',
dataPath: '.include',
message: 'should be array',
},
]);
});
it('leaves out "include" for models with no relations', () => {
const filterProperties = Object.keys(orderFilterSchema.properties || {});
expect(filterProperties).to.not.containEql('include');
});
it('describes "offset" as an integer', () => {
const filter = {offset: 'invalid-offset'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
keyword: 'type',
dataPath: '.offset',
message: 'should be integer',
},
]);
});
it('describes "limit" as an integer', () => {
const filter = {limit: 'invalid-limit'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
keyword: 'type',
dataPath: '.limit',
message: 'should be integer',
},
]);
});
it('describes "skip" as an integer', () => {
const filter = {skip: 'invalid-skip'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
keyword: 'type',
dataPath: '.skip',
message: 'should be integer',
},
]);
});
it('describes "order" as an array', () => {
const filter = {order: 'invalid-order'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
keyword: 'type',
dataPath: '.order',
message: 'should be array',
},
]);
});
function expectSchemaToAllowFilter<T>(schema: JsonSchema, value: T) {
const isValid = ajv.validate(schema, value);
const SUCCESS_MSG = 'Filter instance is valid according to Filter schema';
const result = isValid ? SUCCESS_MSG : ajv.errorsText(ajv.errors!);
expect(result).to.equal(SUCCESS_MSG);
}
});
describe('getWhereJsonSchemaFor', () => {
let ajv: Ajv.Ajv;
let customerWhereSchema: JsonSchema;
beforeEach(() => {
ajv = new Ajv();
customerWhereSchema = getWhereJsonSchemaFor(Customer);
});
it('produces a valid schema', () => {
const isValid = ajv.validateSchema(customerWhereSchema);
const SUCCESS_MSG = 'Where schema is a valid JSON Schema';
const result = isValid ? SUCCESS_MSG : ajv.errorsText(ajv.errors!);
expect(result).to.equal(SUCCESS_MSG);
});
});
@model()
class Order extends Entity {
@property({id: true})
id: number;
@property()
customerId: number;
}
@model()
class Customer extends Entity {
@property({id: true})
id: number;
@property()
name: string;
@hasMany(() => Order)
orders?: Order[];
}