Skip to content

Commit 82ef803

Browse files
fix for having a proper json schema for "where"
Signed-off-by: Durgesh Rathod <[email protected]>
1 parent ff4dd06 commit 82ef803

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

packages/repository-json-schema/src/filter-json-schema.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,22 +178,51 @@ export function getFilterJsonSchemaFor(
178178
*
179179
* @param modelCtor - The model constructor to build the filter schema for.
180180
*/
181-
export function getWhereJsonSchemaFor(
182-
modelCtor: typeof Model,
183-
options: FilterSchemaOptions = {},
184-
): JsonSchema {
185-
const schema: JsonSchema = {
186-
...(options.setTitle !== false && {
187-
title: `${modelCtor.modelName}.WhereFilter`,
188-
}),
181+
function getWhereJsonSchemaFor(modelCtor, options = {}) {
182+
const props = modelCtor.definition.properties;
183+
184+
const schema = {
185+
$schema: 'http://json-schema.org/draft-07/schema#',
189186
type: 'object',
190-
// TODO(bajtos) enumerate "model" properties and operators like "and"
191-
// See https://github.com/loopbackio/loopback-next/issues/1748
192-
additionalProperties: true,
187+
properties: {},
188+
additionalProperties: false,
189+
$defs: {
190+
operatorObject: {
191+
type: 'object',
192+
properties: {
193+
eq: { type: 'any' },
194+
neq: { type: 'any' },
195+
gt: { type: 'any' },
196+
gte: { type: 'any' },
197+
lt: { type: 'any' },
198+
lte: { type: 'any' },
199+
inq: { type: 'array', items: { type: 'any' } },
200+
nin: { type: 'array', items: { type: 'any' } },
201+
between: { type: 'array', items: { type: 'any' }, minItems: 2, maxItems: 2 },
202+
like: { type: 'string' },
203+
nlike: { type: 'string' },
204+
ilike: { type: 'string' },
205+
nilike: { type: 'string' },
206+
regexp: { type: 'string' },
207+
},
208+
additionalProperties: false,
209+
},
210+
},
193211
};
194212

195-
return schema;
196-
}
213+
for (const propName in props) {
214+
const propDef = props[propName];
215+
const baseType = typeof propDef.type === 'function'
216+
? propDef.type.name.toLowerCase()
217+
: 'string';
218+
219+
schema.properties[propName] = {
220+
anyOf: [
221+
{ type: baseType },
222+
{ $ref: '#/$defs/operatorObject' },
223+
],
224+
};
225+
}
197226

198227
/**
199228
* Build a JSON schema describing the format of the "fields" object

0 commit comments

Comments
 (0)