Tag: jsonschema

mongoose里的2个混合对象的Schema数组

嘿我试图做一个数组到一个数组,它们都是混合types,多数民众赞成我是如何build立它 var Fields = new Schema({ type: {type: String}, length: {type: Number}, lable: {type: String}, name: {type: String} }); var Fieldsets = new Schema({ label: {type: String}, name: {type: String}, fields: {type: [Fields]} }); var Steps = new Schema({ order: {type: Number}, name: {type: String}, fieldsets: {type: [Fieldsets]} }); var FormSchema = new Schema({ name: […]

从JSON模式自动生成JavaScript

经过一小时的search,我发现没有解决我的问题,现在我希望有人能够帮助我在这里。 所以基本上我正在寻找的是一种从JSON(Schema)文件自动使用NodeJS生成JavaScript文件(* .js)的方法。 我知道有一些像fs.write这样的东西,但是我认为这对于我的问题肯定是不合适的。 到目前为止,我没有find其他的方式来创build我的JavaScript文件以外的。 基本上我想翻译: { "type":"object", "properties": { "name": { "type": "string" }, "age": { "type": "integer", "default":12 }, "favorite_color": { "type": "string" }, "gender": { "type": "string", "enum": [ "male", "female" ] } } } 进入JavaScript代码如: var data = function() { data.baseConstructor.call(this); this.name = ko.observable(""); this.age = ko.obseravble(12); this.favorite_color = ko.observable(); this.gender […]

如何通过HTTP执行jsonschemavalidation?

我已经定义了一个restservice并试图做一个jsonschemavalidation与文书工作: var _ = require('underscore'); var validate = require('isvalid-express'); var paperwork=require('paperwork'); module.exports = function (app) { app.post('/myroute', paperwork({ username: /[a-z0-9]+/, password: String, age: Number, interests: [String], jobs: [{ company: String, role: String }] }, function (req, res) { // … })); }; 这是我邮寄给myroute的请求: { username: 'brucewayne', password: 'iambatman', age: 36, interests: ['Climbing', 'CQC', 'Cosplay'], jobs: […]

1或JSON模式中的一组属性

我的JSON数据模型有一个对象,其中只有一个或一个属性,以及其他属性都有自己的约束。 有没有办法做到这一点,而不是在这个例子中重复? 下面是一个简单的例子,就像我已经用Node.js + ajv实现这个一样。 var Ajv = require('ajv'), ajv = new Ajv(); var schema = { type: 'object', properties: { id: {type: 'string'}, a: {type: 'integer'}, b: {type: 'integer'} }, required: ['id'], oneOf: [ {required: ['a']}, {required: ['b']} ], additionalProperties: false }; // invalid var json1 = { id: 'someID', a: 1, b: 3 […]

为什么Ajv在编译期间无法parsing引用?

以下是我试图编译和用于validation的JSON模式的示例。 为了完成这个,我使用'ajv'npm模块 。 这是我正在运行的代码… var ajv = require('ajv')(); var contactSchema = { "$schema": "http://json-schema.org/draft-04/schema#", "title": "Contact", "type": "object", "additionalProperties": false, "properties": { "work": { "$ref": "#definitions/phone" }, "home": { "$ref": "#definitions/phone" }, }, "definitions": { "phone": { "type": "object", "required": ["number"], "properties": { "number": { "type": "string" }, "extension": { "type": "string" } } } […]

用于json对象的NodeJSvalidation库

我需要在我的NodeJS应用程序中validation一些对象。 我已经使用了一个很棒的库express-validator ,它完美的工作,但是现在我需要validation不同的对象,不仅仅是请求,而且就expression式validationvalidator ,它使用validationvalidator库,而不是支持stringtypes以外的types。 我发现不同的变体,如Jsonschema , Ajv 他们提供了很好的function,但我需要能够设置错误消息,而不是只是捕获exception或从返回对象parsing它。 像那样 var schema = { "id": "/SimplePerson", "type": "object", "properties": { "name": {"type": "string", "error": "A name should be provided"}, "address": {"$ref": "/SimpleAddress"}, "votes": {"type": "integer", "minimum": 1} } }; 所以我可以为每个属性设置一个错误消息。 有没有现成的解决scheme来实现这个function? 可能的解决scheme 我find了一个伟大的图书馆JSEN它提供了必要的function。

JSON模式给我multipleOf 0.01validation错误的任何数字以.49或.99结尾

我正在使用JSON模式来validation服务器请求,我有一些值,我想validation2DP。 我已经使用下面的模式来validation这些字段: 'properties': { 'amount': {'type': ['number', 'null'], 'multipleOf': 0.01} } 这适用于以.49或.99结尾的数字以外的所有情况,其中我得到的错误amount is not a multiple of (divisible by) 0.01整数amount is not a multiple of (divisible by) 0.01 。 这大概是某种浮点错误。 如果这是不正确的,我应该如何validation数字到一定的精度?

节点表示JSON-Schema多字段validation

使用express-jsonschema 如何validation两个字段,例如: ("quantity" == 0 && "actualQuantity" == 0) || ("quantity" > 0 && "actualQuantity" > 0)

用JSON-Schema定义一个模式并使用Mongoose?

你好,我有一个关于mongoose的症结。 有一种使用Mongoose Schema的JSON-Shema的方法吗? 假设我想用像JSON-Schema这样的标准来定义我的API数据模式,因为它很好。 似乎我需要再次定义它时,我想使用Mongoose / MongoDB! 这是我想避免的一些难看的重复。 理想情况下,更改JSON模式定义也会更改MongoDB模式。 如果我使用JOI.JSvalidation库,会出现类似的问题。 有没有人find解决scheme? 还是有另一种方法? 谢谢

如何pipe理多个JSON模式文件?

我试图使用commonjs-utils中的node.js + json-schema.js来validation我的JSON API。 只是单一的validation很容易,但找不到正确的方式来pipe理多个模式文件以引用对方。 假设我有两个模型和两个API。 // book { "type": "object", "properties": { "title": { "type": "string" }, "author": { "type": "string" } } } // author { "type": "object", "properties": { "first_name": { "type": "string" }, "last_name": { "type": "string" } } } // authors API { "type": "array", "items": { "$ref": "author" } […]