Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Ajv.js一次编译多个json模式_Javascript_Node.js_Express_Jsonschema_Ajv - Fatal编程技术网

Javascript 使用Ajv.js一次编译多个json模式

Javascript 使用Ajv.js一次编译多个json模式,javascript,node.js,express,jsonschema,ajv,Javascript,Node.js,Express,Jsonschema,Ajv,我一直在寻找一种使用ajv(另一个json模式验证器)同时编译多个json模式的方法。我已经尝试过以下方法,它是有效的(下面的代码示例),但我不确定它是否是正确的方法,因为根据这里的AJVAPI 函数compile定义为.compile(对象模式)->Function 这个定义没有提到接受布尔值作为参数,但是当我尝试在没有任何参数的情况下使用ajv.compile() 错误:架构应为对象或布尔值 但是在调用ajv.compile(true)之后,代码运行时没有任何错误,我猜传递true作为参数意

我一直在寻找一种使用ajv(另一个json模式验证器)同时编译多个json模式的方法。我已经尝试过以下方法,它是有效的(下面的代码示例),但我不确定它是否是正确的方法,因为根据这里的AJVAPI

函数compile定义为.compile(对象模式)->Function

这个定义没有提到接受布尔值作为参数,但是当我尝试在没有任何参数的情况下使用
ajv.compile()

错误:架构应为对象或布尔值

但是在调用
ajv.compile(true)
之后,代码运行时没有任何错误,我猜传递
true
作为参数意味着编译选项中定义的所有模式,但是正如我所说,我在avs文档中找不到关于我的这个假设的任何信息。(我在schemas.js文件中定义了我的模式)

这是编译多个模式的正确方法吗

var express = require('express');
var router = express.Router();

const schemas = require('../schemas.js');

var Ajv = require('ajv');
var ajv = new Ajv({
    allErrors: true,
    schemas: [schemas.profile, schemas.vzor]
});

var validate = ajv.compile(true);

router.post('/schema_test/', function (req, res, next) {

    var valid = ajv.validate('profile', req.body);
    if (valid) 
        console.log('Valid!');
    else
        console.log('Invalid: ' + ajv.errorsText(validate.errors));

    return res.sendStatus(200);
});
你看到那张照片了吗?在这里可以看到下面的代码段:

var模式={
“$id”:”http://example.com/schemas/schema.json",
“类型”:“对象”,
“财产”:{
“foo”:{“$ref”:“defs.json#/definitions/int”},
“bar”:{“$ref”:“defs.json#/definitions/str”}
}
};
var defschema={
“$id”:”http://example.com/schemas/defs.json",
“定义”:{
“int”:{“type”:“integer”},
“str”:{“type”:“string”}
}
};
var ajv=新ajv;
var validate=ajv.addSchema(defschema)
.编译(模式);
我相信当您调用
ajv.compile(true)
时,
true
被视为一个额外的有效模式,它接受除空消息之外的所有内容。因此,我认为您不希望将
true
传递到
compile()