最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - validating json with json schema in angular 6 - Stack Overflow

matteradmin5PV0评论

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
Add a ment  | 

1 Answer 1

Reset to default 4

you 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);
Post a comment

comment list (0)

  1. No comments so far