javascript - JSON 数据验证失败

标签 javascript node.js json

我没有通过 JSON 数据验证测试,我应该在其中创建一个 JSON 对象 persons,其属性包括 Name、EmployeeID、Experience、Company 和 Designation,并使用循环访问它。 我只是在学习 JSON,我认为问题在于它也需要 nodejs 的知识 这是 json 文件 (data.json)

'{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}'

这是js文件:

let jsonData = require('./data.json');
let persons=JSON.parse('./data.json', jsonData);
for(i in persons){
    console.log(persons.i);
}

这是验证文件:

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")
}

});

我收到 JSON 数据无效。状态:失败

最佳答案

根据您需要创建的内容的描述,您似乎需要这些对象的数组

所以,JSON 应该是

[{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}]

那么“JS”就是

let persons=require('./data.json');
for(let i in persons){
    console.log(persons[i]);
}

验证器将是

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

{"persons":{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}}

关于javascript - JSON 数据验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58355142/

相关文章:

javascript - 将两个数组组合成 JSON 中的一个对象

javascript - Node :Error 401(Unauthorized) i m using passport-jwt

node.js - npm run build 在本地机器上运行良好,但在远程 ubuntu 服务器上显示错误

.net - JSON 对象数组到 .NET 对象

node.js - 如何从远程服务器调用本地运行的Web服务器

javascript - MongoDB 不限于查询字段

javascript - 如何检查对象是否不具有函数参数中提到的属性?

javascript - 有没有办法自动创建用于语言翻译的 .json 文件?

javascript - 链式 promise 不会在拒绝时传递

javascript - 如何选择 CDN 来加载 Javascript 和 CSS 库?