admin管理员组文章数量:1434439
This is the first time i m working on json schema validation in Angular project.I need help to validate JSON(from REST API) with JSON schema. Below is sample json schema(generated online)
{
"$schema": "",
"type": "object",
"properties": {
"specifications": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"templateName": {
"type": "string"
}
},
"required": [
"templateName"
]
},
{
"type": "object",
"properties": {
"services": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"servicename": {
"type": "string"
},
"servicedescription": {
"type": "string"
}
},
"required": [
"servicename",
"servicedescription"
]
}
]
}
},
"required": [
"services"
]
},
{
"type": "object",
"properties": {
"javaplatform": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"javaversion": {
"type": "string"
}
},
"required": [
"javaversion"
]
}
]
}
},
"required": [
"javaplatform"
]
}
]
}
},
"required": [
"specifications"
]
}
Below is my sample json
{
"specifications": [
{
"templateName": "specifications"
},
{
"services": [
{
"servicename": "name",
"servicedescription": "description"
}
]
},
{
"javaplatform": [
{
"javaversion": "1.8"
}
]
}
]
}
Kindly let me know how to validate json in angular6/javascript/jquery?
Thanks
This is the first time i m working on json schema validation in Angular project.I need help to validate JSON(from REST API) with JSON schema. Below is sample json schema(generated online)
{
"$schema": "http://json-schema/draft-04/schema#",
"type": "object",
"properties": {
"specifications": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"templateName": {
"type": "string"
}
},
"required": [
"templateName"
]
},
{
"type": "object",
"properties": {
"services": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"servicename": {
"type": "string"
},
"servicedescription": {
"type": "string"
}
},
"required": [
"servicename",
"servicedescription"
]
}
]
}
},
"required": [
"services"
]
},
{
"type": "object",
"properties": {
"javaplatform": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"javaversion": {
"type": "string"
}
},
"required": [
"javaversion"
]
}
]
}
},
"required": [
"javaplatform"
]
}
]
}
},
"required": [
"specifications"
]
}
Below is my sample json
{
"specifications": [
{
"templateName": "specifications"
},
{
"services": [
{
"servicename": "name",
"servicedescription": "description"
}
]
},
{
"javaplatform": [
{
"javaversion": "1.8"
}
]
}
]
}
Kindly let me know how to validate json in angular6/javascript/jquery?
Thanks
Share Improve this question edited Jun 21, 2019 at 8:14 Ghoul Ahmed 4,8342 gold badges17 silver badges25 bronze badges asked Jun 21, 2019 at 7:50 DeeDee 1933 silver badges14 bronze badges 1- https://json-schema/implementations.html#validators – youri Commented Jun 21, 2019 at 8:19
1 Answer
Reset to default 4you can try Ajv
here is example code
import * as Ajv from 'ajv'
var ajv = new Ajv();
var validate = ajv.pile(schema);
var valid = validate(data);
if (!valid) console.log(validate.errors);
本文标签: javascriptvalidating json with json schema in angular 6Stack Overflow
版权声明:本文标题:javascript - validating json with json schema in angular 6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745624298a2666875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论