Skip to content

Commit

Permalink
fix: @loopback/rest - validation/request-body.validator
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Mifek <jakub.mifek@lutherx.com>
  • Loading branch information
Jakub Mifek authored and dhmlau committed Feb 14, 2022
1 parent ce22358 commit 58bb8e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Expand Up @@ -105,6 +105,37 @@ describe('Validation at REST level', () => {
});
});

context('with optional body', () => {
before(() => {
const schema: SchemaObject = jsonToSchemaObject(
getJsonSchema(Product, {exclude: ['id']}),
);

class ProductController {
@post('/products')
async create(
@requestBody(aBodySpec(schema))
data: object = {id: 1, name: 'a-product-name', price: 100},
): Promise<Product> {
return new Product(data);
}
}

return givenAnAppAndAClient(ProductController);
});
after(() => app.stop());

it('accepts undefined', async () => {
const {body} = await client
.post('/products')
.type('json')
.send(undefined)
.expect(200);

expect(body).to.containEql({id: 1, name: 'a-product-name', price: 100});
});
});

// This is the standard use case that most LB4 applications should use.
// The request body specification is inferred from a decorated model class.
context('for request body specified via model definition', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/rest/src/validation/request-body.validator.ts
Expand Up @@ -56,6 +56,7 @@ export async function validateRequestBody(
);
throw err;
}
if (!required && !body.value) return;

const schema = body.schema;
/* istanbul ignore if */
Expand Down

0 comments on commit 58bb8e4

Please sign in to comment.