Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

node.js - How to use AJV to validate query object that contains an array of objects

I want to validate query params that come in a request using ajv, the problem I'm having is the query includes an array of objects and obviously arrays have a special kind of encoding when it comes to sending them in the query part of the request.

So here is my ajv schema

{
        type: 'object',
        required: [
            'filters',
        ],
        properties: {
            filters: {
                type: 'object',
                required: ['filtrationEntities'],
                properties: {
                    filtrationEntities: {
                        type: 'array',
                        items: {
                            type: 'object',
                            required: ['id', 'name'],
                            properties: {
                                name: {
                                    type: 'string',
                                    enum: ['compound', 'city', 'district'],
                                    errorMessage: 'The name must have a value of compound, city or district',
                                },
                                id: {
                                    type: 'string', 
                                    validator: ([validateObjectId, 'Entity id must be a valid object id']),
                                },
                            },
                        },
                        minItems: 1,
                        maxItems: 5,
                        uniqueItems: true,
                        errorMessage: {
                            maxItems: 'Maximum allowed number of entities is 5',
                            uniqueItems: 'A duplicate entity was sent',
                        },
                    },
                 
                },
            },
        },
    }

and here is the error details:

[
  {
    keyword: 'type',
    dataPath: '/filters/filtrationEntities',
    params: { type: 'array' },
    message: 'should be array'
  }
]

Here is the query object that I'm trying to send

{
  "filters": {
    "filtrationEntities": [
      {
        "name": "compound",
        "id": "600af0e0849dc907bc432a81"
      },
      {
        "name": "compound",
        "id": "5f3a9859a8e101006d245107"
      }
    ]
  }
}

I'm using express for my server setup so here is how it receives the query object

{ filters: { filtrationEntities: { name: [Array], id: [Array] } } }

For some reason express turn filtrationEntites into an object and turns each field into an array

All these data are derived from a test suite I'm running using mocha and here is the test code if it helps

 const{ body: { listingsStatistics }, status } = await request(app)
                .get(`/apis/agents/listings/statistics`)
                .set('x-auth-token', token)
                .query({ 
                    filters: { 
                        filtrationEntities: compoundIds.map(_id => {return { name: 'compound', id: _id };}),
                    }, 
                });
question from:https://stackoverflow.com/questions/65830043/how-to-use-ajv-to-validate-query-object-that-contains-an-array-of-objects

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...