首页 > 解决方案 > Ajv 验证始终返回 true

问题描述

我尝试验证JSON现有 JSON 模式的数据。

我试过了

const filename = path.join(__dirname, 'google-wallet-object-schema.json') // from https://walletobjects.googleapis.com/$discovery/rest?version=v1
const schemas = require(filename)

const ajv = new Ajv({
    schemaId: 'auto',  
    additionalProperties: false,
    $data: true, 
    // unknownFormats: 'ignore',
    allErrors: true,
    validateSchema: true, 
    format: 'full', 
    //jsonPointers :true
    /* schemas: schemas.schemas */
})

ajv.addSchema(schemas.schemas).compile(schemas.resources);
const v = ajv.getSchema('#/flightobject/methods/insert')
const test = v({ dddd: '1' })
console.log(test)
console.log(ajv.errors)

我期望false但验证总是返回true有谁知道我在这里做错了什么?

标签: javascriptnode.jsjsonjsonschemaajv

解决方案


该 JSON 文档不是 JSON 模式。

根据https://developers.google.com/discovery

基于 JSON Schema的受支持 API 模式目录。

每个受支持 API 的机器可读“发现文档”。

您必须要么使用他们提供的客户端库,要么编写自己的“发现文档”处理器。


推荐阅读