Çok başarılı bir doğrulayıcı. Örneğim olsun deyyu ekliyorum:
https://github.com/mafintosh/is-my-json-valid
var trueString = {
required: true,
type: 'string'
},
falseString = {
required: false,
type: 'string'
},
trueBoolean = {
"type": "boolean",
required: true
},
falseBoolean = {
type: 'boolean',
required: true
},
trueNumber = {
type: 'number',
required: true
},
trueInteger = {
type: 'integer',
required: true
};
var boardSchema = {
required: true,
type: 'object',
properties: {
name: trueString,
id: {
type: 'integer',
format: 'sadeceSifir'
}
}
},
digerBoardSchema = {
required: true,
type: 'object',
properties: {
name: trueString,
id: {
type: 'integer',
format: 'sadeceSifir'
}
}
}
var validate = validator(
{ $ref: '#digeri' },
{
schemas: { board: boardSchema, digeri: digerBoardSchema },
formats: { sadeceSifir:/^0$/, sadece1:/^1$/ }
}
)
console.log(validate({ name: 'New board', id: 0 }));
Diğer örnek:
var validator = isMyJsonValid
var boardSchema = {
required: true,
type: 'object',
properties: {
name: {
required: true,
type: 'string'
},
id: {
type: 'integer',
format: 'sadece1'
},
columns: {
type: 'array'
},
roles: {
type: 'array'
},
users: {
type: 'object',
required: true,
properties: {
invitees: {
type: 'array'
},
members: {
type: 'array'
}
}
},
}
};
var schemaFormats = {
schemas: {
board: boardSchema
},
formats: {
sadeceSifir: /^0$/,
sadece1: /^1$/,
'date-time': /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}[tT ]\d{2}:\d{2}:\d{2}(\.\d+)?([zZ]|[+-]\d{2}:\d{2})$/,
date: /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}$/
}
}
var validate = validator({
$ref: '#board'
}, schemaFormats)
console.log(validate({
name: 'New board',
id: 1,
users:{invitees1:[], members:[]}
}));
Node.js içinde kullanmak için yazdım
var validator = require('is-my-json-valid');
function f_validate(_obj, _schemaName) {
var schema_member = {
},
schema_users = {
type: 'object',
required: true,
properties: {
invitees: {
type: 'array'
},
members: {
type: 'array'
}
}
},
schema_board = {
required: true,
type: 'object',
properties: {
name: {
required: true,
type: 'string'
},
id: {
type: 'integer',
format: 'sadece1'
},
columns: {
type: 'array'
},
roles: {
type: 'array'
},
users: schema_users
}
};
var schemaFormats = {
schemas: {
board: schema_board,
users: schema_users,
member: schema_member
},
formats: {
sadeceSifir: /^0$/,
sadece1: /^1$/,
'date-time': /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}[tT ]\d{2}:\d{2}:\d{2}(\.\d+)?([zZ]|[+-]\d{2}:\d{2})$/,
date: /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}$/
}
}
var validate = validator({$ref: '#' + _schemaName}, schemaFormats);
var result = validate(_obj);
console.log(validate.errors);
return result;
}
module.exports = f_validate;
Bu daha iyisi:
var validator = require('is-my-json-valid');
var schema_member = {
},
schema_users = {
type: 'object',
required: true,
properties: {
invitees: {
type: 'array'
},
members: {
type: 'array'
}
}
},
schema_board = {
required: true,
type: 'object',
properties: {
name: {
required: true,
type: 'string'
},
id: {
type: 'integer',
format: 'sadece1'
},
columns: {
type: 'array'
},
roles: {
type: 'array'
},
users: schema_users
}
},
schemaFormats = {
schemas: {
board: schema_board,
users: schema_users,
member: schema_member
},
formats: {
sadeceSifir: /^0$/,
sadece1: /^1$/,
'date-time': /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}[tT ]\d{2}:\d{2}:\d{2}(\.\d+)?([zZ]|[+-]\d{2}:\d{2})$/,
date: /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}$/
}
}
function f_filterAndValidate(_obj, _schemaName) {
this.schemaName = _schemaName;
this.obj = _obj;
this.f_filter = function () {
var filter = validator.filter({$ref: '#' + this.schemaName}, schemaFormats)
return filter(this.obj);
};
this.f_validate = function (_filtered_object) {
var validate = validator({$ref: '#' + this.schemaName}, schemaFormats);
var result = validate(_filtered_object);
console.log(validate.errors);
return result;
};
return this.f_validate(this.f_filter())
}
module.exports = {
schemas_board: 'board',
schemas_users: 'users',
'f_filterAndValidate': f_filterAndValidate
};