Loopback 4 How to validate requestBody properties

Felix Movee

I'm looking for a way to prevent unwanted properties to be present in the requestBody as described in the associated Model

Here is my model :

import { Model, model, property } from '@loopback/repository';

@model({
   name: 'AwsS3',
   strict: true,
   description: 'AWS S3 Object description',
   properties: {
   Key: {
      type: 'String',
      required: 'true',
   },
   Bucket: {
      type: 'String',
      requied: 'true',
   },
 },
})
export class AwsS3 extends Model {
@property({
   type: 'string',
   description: 'path/to/file',
   required: true,
}) Key: string;

@property({
   type: 'string',
   description: 'AWS-S3-Bucket-Name',
   required: true,
})
Bucket: string;

constructor(data: AwsS3) {
  super(data);
 }
}

I used it like this in the controller

 function(@requestBody({
    required: true,
    description: 'aws object settings',
    content: {
       'application/json': {},
     },
   }) body : AwsS3
 ){
    console.log(body);
 }

It throws correctly when one of both properties is missing or in the wrong type. But if i send a json like bellow nothing is thrown and object is processed with the UnwantedProp

{
    Key: 'key',
    Bucket : 'bucket',
    UnwantedProp: 40
}
Felix Movee

I found it to be achievable by using the @api decorator and setting the additionalProperties: false from the openapi specs.

use it like :

 @api(
    basePath: '/',
    paths : {
       'somepath': {
           'post' : {
               'x-operation-name': 'myfunction',
               'x-controller-name': 'MyController',
               // properties for route
               requestBody: {
                  required: true,
                  content: {
                    'application/json': {
                       schema: {
                         type: 'object',
                         additionalProperties: false, // <=== here it is
                         properties: {
                            Key: { type: 'string'},
                            Bucket: {type: 'string'},
                         },
                         required: ['Bucket', 'Key'],
                       },
                   },
                 },
               },
            }
        }
    }
 )
 export class MyController{
    async myfunction(
       @requestBody({ settings: { strict: true } }) body
    ){}
 }

when testing it throws the following as expected :

{
    "error": {
        "statusCode": 422,
        "name": "UnprocessableEntityError",
        "message": "The request body is invalid. See error object `details` property for more info.",
        "code": "VALIDATION_FAILED",
        "details": [
            {
                "path": "",
                "code": "additionalProperties",
                "message": "should NOT have additional properties",
                "info": {
                    "additionalProperty": "unwantedProp"
                }
            }
        ]
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the best practice of creating requestBody in loopback 4?

How to protect properties for different roles using loopback

Springboot - validate @RequestBody

how to validate properties key in a geojson

how do I set properties of a v4l2loopback device and make them visible to my web browser?

Is it possible to make hasMany relations behave like actual properties in LoopBack 4

How to watch for file changes in Loopback 4?

How to recreate 'dist' folder in Loopback 4

How to specify environment variables in loopback 4?

Loopback 4: How to query an array of objects

How to add morgan to Loopback 4 RestApplication?

How to perform a post request with loopback 4

How to set a different Http Status in loopback 4

How should Model Validation be implemented in Loopback 4?

How to integrate pubnub in loopback4

How to implement search and filter operations in loopback 4?

How to set float type in loopback 4

How to use MongoDB extended operators in Loopback 4?

How to use Jackson to validate duplicated properties?

How to validate properties/custom configuration files?

How to conditionally validate properties using FluentValidation?

How to validate required fields in class properties?

How to validate nullable object variable with properties

How to validate scorm package properties upon upload?

How to specify minimum and maximum length for fields in Loopback 4?

How to add self-signed ssl certificate for loopback4?

How to start HTTP Server on all network interfaces in LoopBack 4

How can I increment a counter variable in LoopBack 4 with a MongoDB datasource?

How to customize the api version that appear in loopback 4 generated API explorer?