NodeJSON架构无法读取架构

NodeJSON架构无法读取架构,json,node.js,rest,express,jsonschema,Json,Node.js,Rest,Express,Jsonschema,我有一个express应用程序,它有一个post方法(post是json类型): js(简化版): js包含对json的验证: var jsonschemavalidate = require("json-schema"); var basicSchema = require('fs').readFileSync('./schema.json', 'utf8'); exports.validate = function (event) { console.log(jsonschemav

我有一个express应用程序,它有一个post方法(post是json类型):

js(简化版):

js包含对json的验证:

var jsonschemavalidate = require("json-schema");
var basicSchema = require('fs').readFileSync('./schema.json', 'utf8');

exports.validate = function (event) {
    console.log(jsonschemavalidate.validate(event, basicSchema).errors);
}
schema.json:

{ 
    name : "test",
    type : 'object', 
    properties : { 
        event_id : { type : 'string' }, 
        timestamp : { type : 'string' } 
    }
}
对于输入,我使用curl:

curl -i -X POST -H 'Content-Type: application/json' -d '{"event_id": "NedaleGassss", "timestamp": "a2009321"}' http://localhost:3000/listener/v1/event/
结果如下:

[ { property: '',
    message: 'Invalid schema/property definition {\n    name : "test",\n    type : "object",\n    additionalProperties : false,\n    properties :\n    {\n        event_id            : { type : "string" },\n        timestamp        \t: { type : "string" }\n    }\n}' } ]

正如错误所说,您的模式无效。模式也应该是有效的JSON, 因此,属性和字符串应使用双引号:

{ 
  "name" : "test",
  "type" : "object", 
  "properties"  : { 
    "event_id"  : { "type" : "string" }, 
    "timestamp" : { "type" : "string" } 
  }
}
这应该可以奏效(除非你在过去一年已经弄明白了)

而且:

var basicSchema = require('fs').readFileSync('./schema.json', 'utf8');
可能被以下内容取代:

var basicSchema = require('./schema');

他们怎么会失败?也就是说,您得到的错误是什么?@jsalonen我添加了输入和输出。看起来您正在向验证器传递一个字符串,它应该是JSON。查看json模式项目中的测试,您可以看到他们在调用validate之前使用了json.parse(str)。
var basicSchema = require('./schema');