AJV 的验证器 returns 始终为真值
AJV's validator returns always true value
我需要通过以下方式验证 JSON 个文件:
const setupSchema = fs.readFileSync(schemaDir +'/setup.json');
正在编译:
const setupValidator = ajv.compile(setupSchema);
我的问题是那一行:
console.log( setupValidator('') );
总是returns true
即使验证器的参数像上面一样是空字符串。我想加载的方式很糟糕但是......需要问比我更聪明的人。
// You should specify encoding while reading the file otherwise it will return raw buffer
const setupSchema = fs.readFileSync(schemaDir +'/setup.json', "utf-8");
// setupSchema is a JSON string, so you need to parse it before passing it to compile as compile function accepts an object
const setupValidator = ajv.compile(JSON.parse(setupSchema));
console.log( setupValidator('') ) // Now, this will return false;
除了上面的操作,您还可以使用 require
.
简单地要求 json 文件
const setupSchema = require(schemaDir +'/setup.json');
const setupValidator = ajv.compile(setupSchema);
console.log( setupValidator('') );
来自快速入门指南:(http://json-schema.org/)
The JSON document being validated or described we call the instance,
and the document containing the description is called the schema.
The most basic schema is a blank JSON object, which constrains
nothing, allows anything, and describes nothing:
{}
You can apply constraints on an instance by adding validation keywords
to the schema. For example, the “type” keyword can be used to restrict
an instance to an object, array, string, number, boolean, or null:
{ "type": "string" }
这意味着如果您的架构是一个空对象或不使用 JSON 架构词汇表,Ajv 的 compile
函数将始终生成始终通过的验证函数:
var Ajv = require('ajv');
var ajv = new Ajv({allErrors: true});
var schema = {
foo: 'bar',
bar: 'baz',
baz: 'baz'
};
var validate = ajv.compile(schema);
validate({answer: 42}); //=> true
validate('42'); //=> true
validate(42); //=> true
可能您的 setup.json
加载不正确或者不是符合 JSON 架构规范的架构。
我需要通过以下方式验证 JSON 个文件:
const setupSchema = fs.readFileSync(schemaDir +'/setup.json');
正在编译:
const setupValidator = ajv.compile(setupSchema);
我的问题是那一行:
console.log( setupValidator('') );
总是returns true
即使验证器的参数像上面一样是空字符串。我想加载的方式很糟糕但是......需要问比我更聪明的人。
// You should specify encoding while reading the file otherwise it will return raw buffer
const setupSchema = fs.readFileSync(schemaDir +'/setup.json', "utf-8");
// setupSchema is a JSON string, so you need to parse it before passing it to compile as compile function accepts an object
const setupValidator = ajv.compile(JSON.parse(setupSchema));
console.log( setupValidator('') ) // Now, this will return false;
除了上面的操作,您还可以使用 require
.
const setupSchema = require(schemaDir +'/setup.json');
const setupValidator = ajv.compile(setupSchema);
console.log( setupValidator('') );
来自快速入门指南:(http://json-schema.org/)
The JSON document being validated or described we call the instance, and the document containing the description is called the schema.
The most basic schema is a blank JSON object, which constrains nothing, allows anything, and describes nothing:
{}
You can apply constraints on an instance by adding validation keywords to the schema. For example, the “type” keyword can be used to restrict an instance to an object, array, string, number, boolean, or null:
{ "type": "string" }
这意味着如果您的架构是一个空对象或不使用 JSON 架构词汇表,Ajv 的 compile
函数将始终生成始终通过的验证函数:
var Ajv = require('ajv');
var ajv = new Ajv({allErrors: true});
var schema = {
foo: 'bar',
bar: 'baz',
baz: 'baz'
};
var validate = ajv.compile(schema);
validate({answer: 42}); //=> true
validate('42'); //=> true
validate(42); //=> true
可能您的 setup.json
加载不正确或者不是符合 JSON 架构规范的架构。