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 @@ -1049,20 +1049,27 @@ describe('build-schema', () => {
expect(optionalNothingSchema.title).to.equal('Product');
});

it('overrides "partial" option when "optional" options is set', () => {
it('overrides "partial" option when "optional" option is set', () => {
const originalSchema = getJsonSchema(Product);
expect(originalSchema.required).to.deepEqual(['id', 'name']);
expect(originalSchema.title).to.equal('Product');

const optionalNameSchema = getJsonSchema(Product, {
let optionalNameSchema = getJsonSchema(Product, {
partial: true,
optional: ['name'],
});
expect(optionalNameSchema.required).to.deepEqual(['id']);
expect(optionalNameSchema.title).to.equal('ProductOptional[name]');

optionalNameSchema = getJsonSchema(Product, {
partial: false,
optional: ['name'],
});
expect(optionalNameSchema.required).to.deepEqual(['id']);
expect(optionalNameSchema.title).to.equal('ProductOptional[name]');
});

it('uses "partial" option, if provided, when "optional" options is set but empty', () => {
it('uses "partial" option, if provided, when "optional" option is set but empty', () => {
const originalSchema = getJsonSchema(Product);
expect(originalSchema.required).to.deepEqual(['id', 'name']);
expect(originalSchema.title).to.equal('Product');
Expand Down
6 changes: 4 additions & 2 deletions packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export interface JsonSchemaOptions<T extends object> {

/**
* Set this flag to mark all model properties as optional. This is typically
* used to describe request body of PATCH endpoints.
* used to describe request body of PATCH endpoints. This option will be
* overridden by the "optional" option if it is set and non-empty.
*/
partial?: boolean;

Expand All @@ -36,7 +37,8 @@ export interface JsonSchemaOptions<T extends object> {
exclude?: (keyof T)[];

/**
* List of model properties to mark as optional.
* List of model properties to mark as optional. Overrides the "partial"
* option if it is not empty.
*/
optional?: (keyof T)[];

Expand Down