Ajv验证始终返回true。

huangapple go评论69阅读模式
英文:

Ajv validation always returns true

问题

I try to validate JSON data regarding an existing JSON schema.

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

I tried

我尝试过以下代码:

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)

这里我尝试验证 JSON 数据,但是无论如何它都返回 true,我是否有做错什么?

英文:

I try to validate JSON data ragarding an existing JSON schema.

I tried

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)

where I expect false but the validation always returns true does anyone know what I am doing wrong here?

答案1

得分: 0

那个JSON文档不是JSON模式。

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

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

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

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

英文:

That JSON document is not a JSON Schema.

According to https://developers.google.com/discovery

> A directory of supported APIs schemas based on JSON Schema.
>
> A machine-readable "Discovery Document" for each of the supported
> APIs.

You'll have to either use the client libraries they supply, or write your own "Discovery Document" processor.

huangapple
  • 本文由 发表于 2020年1月3日 17:10:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定