Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ describe('filterSchema', () => {
const schema = getFilterSchemaFor(MyUserModel);
expect(MyUserModel.definition.name).to.eql('my-user-model');
expect(schema).to.eql({
title: 'my-user-modelFilter',
properties: {
where: {
type: 'object',
title: 'my-user-modelWhere',
additionalProperties: true,
},
fields: {
type: 'object',
title: 'my-user-modelFields',
properties: {
id: {type: 'boolean'},
age: {type: 'boolean'},
Expand Down
20 changes: 19 additions & 1 deletion packages/repository-json-schema/src/filter-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {JSONSchema6 as JsonSchema} from 'json-schema';
@model({settings: {strict: false}})
class EmptyModel extends Model {}

const scopeFilter = getFilterJsonSchemaFor(EmptyModel);
const scopeFilter = {
...getFilterJsonSchemaFor(EmptyModel),
title: 'ScopeFilter', // TODO(dougal83) base title on model
};

/**
* Build a JSON schema describing the format of the "filter" object
Expand All @@ -22,6 +25,10 @@ const scopeFilter = getFilterJsonSchemaFor(EmptyModel);
*/
export function getFilterJsonSchemaFor(modelCtor: typeof Model): JsonSchema {
const schema: JsonSchema = {
title:
modelCtor.modelName === 'EmptyModel'
? undefined
: `${modelCtor.modelName}Filter`,
properties: {
where: getWhereJsonSchemaFor(modelCtor),

Expand Down Expand Up @@ -58,6 +65,8 @@ export function getFilterJsonSchemaFor(modelCtor: typeof Model): JsonSchema {

if (hasRelations) {
schema.properties!.include = {
// TODO(dougal83) base title on model
title: 'Include',
type: 'array',
items: {
type: 'object',
Expand Down Expand Up @@ -85,6 +94,10 @@ export function getFilterJsonSchemaFor(modelCtor: typeof Model): JsonSchema {
*/
export function getWhereJsonSchemaFor(modelCtor: typeof Model): JsonSchema {
const schema: JsonSchema = {
title:
modelCtor.modelName === 'EmptyModel'
? undefined
: `${modelCtor.modelName}Where`,
type: 'object',
// TODO(bajtos) enumerate "model" properties and operators like "and"
// See https://github.com/strongloop/loopback-next/issues/1748
Expand All @@ -102,7 +115,12 @@ export function getWhereJsonSchemaFor(modelCtor: typeof Model): JsonSchema {

export function getFieldsJsonSchemaFor(modelCtor: typeof Model): JsonSchema {
const schema: JsonSchema = {
title:
modelCtor.modelName === 'EmptyModel'
? undefined
: `${modelCtor.modelName}Fields`,
type: 'object',

properties: Object.assign(
{},
...Object.keys(modelCtor.definition.properties).map(k => ({
Expand Down