tdegrunt / jsonschema

JSON Schema validation
Other
1.83k stars 263 forks source link

Help in validation Json against schema #337

Closed surajprsd closed 3 years ago

surajprsd commented 3 years ago

Hi, I am trying to validate my Json against my Json schema to check if my json has the mandatory fields or not using json schema package.

Below is the code sample. I am assuming the schema is incorrect, if yes please guide on how I can correct this. Thanks in advice.

   var schema = {
        data: {
            type: Object,
            required: true,
            content: [{
                type: Array,
                required: true,
                version: {
                    type: String,
                    required: true
                },
                sequence: {
                    type: Number,
                    required: true
                },
                file: [{
                    type: Array,
                    required: true
                }]
            }
            ]
        },
        "required": ["data"],
        "properties": {
            "data": {
                "type": "object"
            }
        }

    }
    var Validator = require('jsonschema').Validator;
    var v = new Validator();
    var json =
    {
        "data": {
            "contents": [
                {
                    "version": "2021-01-15T16:01:13.475Z",
                    "sequence": 1,
                    "files": [
                        {
                            "fileName": "us-producer-price-index.txt",
                            "name": "us-producer-price-index",
                            "fileSize": "54MB",
                            "fileType": "txt",
                            "lastUpdatedDate": 1610717473000,
                            "fileLength": 56614933,
                            "version": 2
                        }
                    ]
                }
            ]
        }
    };

    console.log(v.validate(json, schema));
surajprsd commented 3 years ago

Appreciate your help in this ! I am new with schemas and would be great if you can modify the above to show validation result which shows a mandatory field missing like filename under files or mandatory version/sequence tag is missing in the json

tdegrunt commented 3 years ago

This is not really the place to ask questions - see https://stackoverflow.com/questions/tagged/jsonschema

surajprsd commented 3 years ago

have already asked but couldn't get my answer on stack overflow