Skip to content
Merged
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 @@ -152,14 +152,24 @@ describe('getFilterJsonSchemaFor', () => {

it('returns "include.title" when no options were provided', () => {
expect(customerFilterSchema.properties)
.to.have.propertyByPath(...['include', 'title'])
.to.have.propertyByPath('include', 'title')
.to.equal('Customer.IncludeFilter');
});

it('returns "include.items.title" when no options were provided', () => {
expect(customerFilterSchema.properties)
.to.have.propertyByPath('include', 'items', 'title')
.to.equal('Customer.IncludeFilter.Items');
});

it('returns "scope.title" when no options were provided', () => {
expect(customerFilterSchema.properties)
.to.have.propertyByPath(
...['include', 'items', 'properties', 'scope', 'title'],
'include',
'items',
'properties',
'scope',
'title',
)
.to.equal('Customer.ScopeFilter');
});
Expand All @@ -185,14 +195,24 @@ describe('getFilterJsonSchemaForOptionsSetTitle', () => {

it('returns "include.title" when a single option "setTitle" is set', () => {
expect(customerFilterSchema.properties)
.to.have.propertyByPath(...['include', 'title'])
.to.have.propertyByPath('include', 'title')
.to.equal('Customer.IncludeFilter');
});

it('returns "include.items.title" when a single option "setTitle" is set', () => {
expect(customerFilterSchema.properties)
.to.have.propertyByPath('include', 'items', 'title')
.to.equal('Customer.IncludeFilter.Items');
});

it('returns "scope.title" when a single option "setTitle" is set', () => {
expect(customerFilterSchema.properties)
.to.have.propertyByPath(
...['include', 'items', 'properties', 'scope', 'title'],
'include',
'items',
'properties',
'scope',
'title',
)
.to.equal('Customer.ScopeFilter');
});
Expand All @@ -205,22 +225,26 @@ describe('getFilterJsonSchemaForOptionsUnsetTitle', () => {
customerFilterSchema = getFilterJsonSchemaFor(Customer, {setTitle: false});
});

it('"title" undefined when a single option "setTitle" is false', () => {
expect(customerFilterSchema.title).to.equal(undefined);
it('no title when a single option "setTitle" is false', () => {
expect(customerFilterSchema).to.not.have.property('title');
});

it('"include.title" undefined when single option "setTitle" is false', () => {
it('no title on include when single option "setTitle" is false', () => {
expect(customerFilterSchema.properties)
.to.have.propertyByPath(...['include', 'title'])
.to.equal(undefined);
.property('include')
.to.not.have.property('title');
});

it('"scope.title" undefined when single option "setTitle" is false', () => {
it('no title on include.items when single option "setTitle" is false', () => {
expect(customerFilterSchema.properties)
.to.have.propertyByPath(
...['include', 'items', 'properties', 'scope', 'title'],
)
.to.equal(undefined);
.propertyByPath('include', 'items')
.to.not.have.property('title');
});

it('no title on scope when single option "setTitle" is false', () => {
expect(customerFilterSchema.properties)
.propertyByPath('include', 'items', 'properties', 'scope')
.to.not.have.property('title');
});
});

Expand Down Expand Up @@ -260,7 +284,7 @@ describe('getWhereJsonSchemaForOptions', () => {
customerWhereSchema = getWhereJsonSchemaFor(Customer, {
setTitle: false,
});
expect(customerWhereSchema.title).to.equal(undefined);
expect(customerWhereSchema).to.not.have.property('title');
});
});

Expand All @@ -283,7 +307,39 @@ describe('getFieldsJsonSchemaFor', () => {
customerFieldsSchema = getFieldsJsonSchemaFor(Customer, {
setTitle: false,
});
expect(customerFieldsSchema.title).to.equal(undefined);
expect(customerFieldsSchema).to.not.have.property('title');
});
});

describe('single option setTitle override original value', () => {
let customerFieldsSchema: JsonSchema;

it('returns builtin "title" when no options were provided', () => {
customerFieldsSchema = {
title: 'Test Title',
...getFieldsJsonSchemaFor(Customer),
};
expect(customerFieldsSchema.title).to.equal('Customer.Fields');
});

it('returns builtin "title" when a single option "setTitle" is set', () => {
customerFieldsSchema = {
title: 'Test Title',
...getFieldsJsonSchemaFor(Customer, {
setTitle: true,
}),
};
expect(customerFieldsSchema.title).to.equal('Customer.Fields');
});

it('returns original "title" when a single option "setTitle" is false', () => {
customerFieldsSchema = {
title: 'Test Title',
...getFieldsJsonSchemaFor(Customer, {
setTitle: false,
}),
};
expect(customerFieldsSchema.title).to.equal('Test Title');
});
});

Expand Down
36 changes: 19 additions & 17 deletions packages/repository-json-schema/src/filter-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {JSONSchema6 as JsonSchema} from 'json-schema';

export interface FilterSchemaOptions {
/**
* Set this flag if you want the schema to include title property.
* Set this flag if you want the schema to set generated title property.
*
* By default the setting is enabled. (e.g. {setTitle: true})
*
Expand All @@ -34,10 +34,9 @@ export function getScopeFilterJsonSchemaFor(

const schema: JsonSchema = {
...getFilterJsonSchemaFor(EmptyModel, {setTitle: false}),
title:
options.setTitle !== false
? `${modelCtor.modelName}.ScopeFilter`
: undefined,
...(options.setTitle !== false && {
title: `${modelCtor.modelName}.ScopeFilter`,
}),
};

return schema;
Expand All @@ -57,8 +56,9 @@ export function getFilterJsonSchemaFor(
options: FilterSchemaOptions = {},
): JsonSchema {
const schema: JsonSchema = {
title:
options.setTitle !== false ? `${modelCtor.modelName}.Filter` : undefined,
...(options.setTitle !== false && {
title: `${modelCtor.modelName}.Filter`,
}),
properties: {
where: getWhereJsonSchemaFor(modelCtor, options),

Expand Down Expand Up @@ -95,12 +95,14 @@ export function getFilterJsonSchemaFor(

if (hasRelations) {
schema.properties!.include = {
title:
options.setTitle !== false
? `${modelCtor.modelName}.IncludeFilter`
: undefined,
...(options.setTitle !== false && {
title: `${modelCtor.modelName}.IncludeFilter`,
}),
type: 'array',
items: {
...(options.setTitle !== false && {
title: `${modelCtor.modelName}.IncludeFilter.Items`,
}),
type: 'object',
properties: {
// TODO(bajtos) restrict values to relations defined by "model"
Expand Down Expand Up @@ -129,10 +131,9 @@ export function getWhereJsonSchemaFor(
options: FilterSchemaOptions = {},
): JsonSchema {
const schema: JsonSchema = {
title:
options.setTitle !== false
? `${modelCtor.modelName}.WhereFilter`
: undefined,
...(options.setTitle !== false && {
title: `${modelCtor.modelName}.WhereFilter`,
}),
type: 'object',
// TODO(bajtos) enumerate "model" properties and operators like "and"
// See https://github.com/strongloop/loopback-next/issues/1748
Expand All @@ -154,8 +155,9 @@ export function getFieldsJsonSchemaFor(
options: FilterSchemaOptions = {},
): JsonSchema {
const schema: JsonSchema = {
title:
options.setTitle !== false ? `${modelCtor.modelName}.Fields` : undefined,
...(options.setTitle !== false && {
title: `${modelCtor.modelName}.Fields`,
}),
type: 'object',

properties: Object.assign(
Expand Down