Skip to content

Commit f0da7dd

Browse files
pnoebauerdhmlau
authored andcommitted
fix: removal of comments and implementation of bug fix
Signed-off-by: Philip Noebauer <[email protected]>
1 parent 82a33cc commit f0da7dd

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

examples/validation-app/src/__tests__/acceptance/validate-discriminator.acceptance.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const validDog = {
2727
},
2828
};
2929

30-
describe('validate properties', () => {
30+
describe('validate properties based on discriminated schemas', () => {
3131
let client: Client;
3232
let app: ValidationApplication;
3333

@@ -49,16 +49,11 @@ describe('validate properties', () => {
4949
await client.post('/pets').send(validDog).expect(200);
5050
});
5151

52-
it('should fail with error indicating the invalid barkVolume', async () => {
52+
it('should fail with error indicating invalid barkVolume type', async () => {
5353
const invalidDog = {...validDog};
5454
invalidDog.animalProperties.barkVolume = 'loud' as unknown as number;
5555
const response = await client.post('/pets').send(invalidDog).expect(422);
5656

57-
/*
58-
below expect statements pass after fixing the bug in function convertToJsonSchema()
59-
loopback-next/packages/rest/src/validation/request-body.validator.ts#lines-87:99
60-
a possible solution is indicated under //NOTE
61-
*/
6257
expect(response.body.error.details.length).to.equal(1);
6358
expect(response.body.error.details[0].message).to.equal('must be number');
6459
expect(response.body.error.details).to.deepEqual([
@@ -72,4 +67,23 @@ describe('validate properties', () => {
7267
},
7368
]);
7469
});
70+
71+
it('should fail with error indicating invalid whiskerLength type', async () => {
72+
const invalidCat = {...validCat};
73+
invalidCat.animalProperties.whiskerLength = 'long' as unknown as number;
74+
const response = await client.post('/pets').send(invalidCat).expect(422);
75+
76+
expect(response.body.error.details.length).to.equal(1);
77+
expect(response.body.error.details[0].message).to.equal('must be number');
78+
expect(response.body.error.details).to.deepEqual([
79+
{
80+
code: 'type',
81+
info: {
82+
type: 'number',
83+
},
84+
message: 'must be number',
85+
path: '/animalProperties/whiskerLength',
86+
},
87+
]);
88+
});
7589
});

packages/rest/src/validation/request-body.validator.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,9 @@ export async function validateRequestBody(
8484
* @param openapiSchema - The OpenAPI schema to convert.
8585
*/
8686
function convertToJsonSchema(openapiSchema: SchemaObject) {
87-
/*
88-
loopback does not pass the options arg to function toJsonSchema(schema, options?),
89-
which is part of the openapi-schema-to-json-schema library;
90-
by default toJsonSchema() removes discriminators, which is why they do not work
91-
in loopback
92-
*/
93-
// TODO: fix below bug; toJsonSchema requires the options object to enable discriminators
94-
const jsonSchema = toJsonSchema(openapiSchema);
95-
// NOTE: replacing above line with below fixes discriminators not working in loopback and all tests pass
96-
// const jsonSchema = toJsonSchema(openapiSchema, {
97-
// keepNotSupported: ['discriminator'],
98-
// });
87+
const jsonSchema = toJsonSchema(openapiSchema, {
88+
keepNotSupported: ['discriminator'],
89+
});
9990

10091
delete jsonSchema['$schema'];
10192
/* istanbul ignore if */

0 commit comments

Comments
 (0)