代码之家  ›  专栏  ›  技术社区  ›  Felix Movee

环回4如何验证请求体属性

  •  0
  • Felix Movee  · 技术社区  · 7 年前

    我正在寻找一种方法来防止不需要的属性出现在请求主体中,如关联模型中所述。

    这是我的模型:

    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);
     }
    }
    

    我在控制器里就是这样用的

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

    当两个属性之一丢失或类型错误时,它将正确抛出。 但是,如果我发送一个类似于bellow的JSON,则不会抛出任何内容,并且对象是用不预先准备的prop处理的。

    {
        Key: 'key',
        Bucket : 'bucket',
        UnwantedProp: 40
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Felix Movee    7 年前

    我发现可以通过使用@api装饰器并从Openapi规范中设置additionalproperties:false来实现。

    像这样使用它:

     @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
        ){}
     }
    

    当测试时,它按预期抛出以下内容:

    {
        "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"
                    }
                }
            ]
        }
    }
    
    推荐文章