[英]failing JSON data validation
I am failing a JSON data validation test, where I am supposed to create a JSON object persons with with properties Name, EmployeeID, Experience, Company and Designation and access it using loop.我在 JSON 数据验证测试失败,我应该在其中创建一个 JSON object具有属性名称、员工 ID、经验、公司和指定的人员并使用循环访问它。 I am just learning JSON and I think the problem is that it requires knowledge of nodejs too here is the json file (data.json)
我只是在学习 JSON,我认为问题在于它也需要 nodejs 的知识,这里是 json 文件(data.json)
'{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}'
Here is the js file:这是js文件:
let jsonData = require('./data.json');
let persons=JSON.parse('./data.json', jsonData);
for(i in persons){
console.log(persons.i);
}
and here is the validation file:这是验证文件:
const Joi = require('joi');
const fss =require('fs');
const schema = Joi.object().keys({
Name: Joi.string().required(),
EmployeeID: Joi.number().required(),
Experience: Joi.number().required(),
Company: Joi.string().required(),
Designation: Joi.string().required()
});
const personSchema=Joi.object().keys({
persons:schema.required()
}).required();
var data;
try{
data = require("./data.json");
}catch(e)
{
data={};
}
var XMLWriter = require('xml-writer');
xw = new XMLWriter;
// You can also pass a callback which will be called synchronously with the validation result.
Joi.validate(data, personSchema, function (err, value) {
if(err==null)
{
console.log("JSON data is valid, Status: Passed");
}else{
console.log("JSON data is invalid. Status: failed")
}
});
I am getting JSON data is invalid.我收到JSON 数据无效。 Status: failed
状态:失败
From the description of what you need to create, it seems you need an Array of those objects根据您需要创建的内容的描述,您似乎需要这些对象的 Array
So, the JSON should be所以,JSON 应该是
[{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}]
Then the "JS" would be那么“JS”将是
let persons=require('./data.json');
for(let i in persons){
console.log(persons[i]);
}
And the validator would be验证器将是
const Joi = require('joi');
const fss = require('fs');
const schema = Joi.object().keys({
Name: Joi.string().required(),
EmployeeID: Joi.number().required(),
Experience: Joi.number().required(),
Company: Joi.string().required(),
Designation: Joi.string().required()
});
const personSchema = Joi.array().items(schema.required()).required();
var data;
try {
data = require("./data.json");
} catch (e) {
data = [];
}
var XMLWriter = require('xml-writer');
xw = new XMLWriter;
// You can also pass a callback which will be called synchronously with the validation result.
Joi.validate(data, personSchema, function (err, value) {
if (err == null) {
console.log("JSON data is valid, Status: Passed");
} else {
console.log(err, "JSON data is invalid. Status: failed")
}
});
if the validator file should be left untouched, then the JSON needs to be as follows
如果验证器文件应该保持不变,那么 JSON 需要如下
{"persons":{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.