0

I am trying to validate JSON (API response) with a JSON schema with

string data = File.ReadAllText(@"C:\Users\Aman.Sharma\Source\Repos\Validator\Validator\testData.json");
string schema = File.ReadAllText(@"C:\Users\Aman.Sharma\Source\Repos\Validator\Validator\V2JsonSchema.json");

var model = JObject.Parse(data);
var json_schema = JSchema.Parse(schema);

bool valid = model.IsValid(json_schema, out IList<string> messages);

But the problem just throws an error with the first occurrence of error and skips the other part. Is there any way to compare the whole JSON with schema and throw errors for each record? Also this method If I change the schema always pass the JSON.

Sample Data

{
  "id": "e1110047-b606-4fb3-84c6-28f7d5456e11",
  "accountInactiveDate": "0001-01-01T00:00:00Z",
  "accountOpenDate": "0001-01-01T00:00:00Z",
  "accountTypeIds": [ 4000 ],
  "address": {
    "city": "vVjEKwUP",
    "addressTypeId": 1000,
    "countryISOCode": "GBR",
    "street1": "xTMksdLL",
    "zipCode": "12345"
  },
  "annualRevenue": 0.0,
  "email": {
    "emailTypeId": 1000,
    "email": "zvvzwsdv@yopmail.com"
  },
  "homeLocationNumber": "316",
  "industryTypeId": 0,
  "isDeleted": false,
  "name": "AccounttxecJizQ",
  "parentAccountId": "00000000-0000-0000-0000-000000000000",
  "phone": {
    "phoneTypeId": 1000,
    "number": "+44 123456"
  },
  "productsTypeIds": [ 3000 ],
  "timeStamp": "\"2e001932-0000-0d00-0000-63315d8e0000\"",
  "links": [
    {
      "href": "https://api-test.QA.cloud/companies/api/v2/accounts/e1110047-b606-4fb3-84c6-28f7d5456e11",
      "rel": "Self"
    }
  ],
  "isBlocked": false,
  "isComplete": true,
  "createDate": "2022-09-26T08:06:38.2263447Z",
  "systemCaller": "qa-user2-automationtests",
  "originSystemCaller": "qa-user2-automationtests",
  "originCreateDate": "2022-09-26T08:06:38.2263447Z"
}

Schema

{
  "type": "object",
  "properties": {
    "searchMode": {
      "enum": [
        "Any",
        "All"
      ],
      "type": "string"
    },
    "queryType": {
      "enum": [
        "Simple",
        "Full"
      ],
      "type": "string"
    },
    "select": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "searchFields": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "orderBy": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "highlightPreTag": {
      "type": "string"
    },
    "highlightPostTag": {
      "type": "string"
    },
    "highlightFields": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "filter": {
      "type": "string"
    },
    "facets": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "scoringProfile": {
      "type": "string"
    },
    "scoringParameters": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "values": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      }
    },
    "page": {
      "format": "int32",
      "type": "integer"
    },
    "pageSize": {
      "format": "int32",
      "type": "integer"
    },
    "includeTotalResultCount": {
      "type": "boolean"
    }
  }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Aman Sharma
  • 311
  • 1
  • 8
  • 22
  • 3
    What is `Data.IsValid`? What JSON Schema library are you using? – Dai Sep 26 '22 at 16:47
  • 1
    Also please provide an example schema and data and try to describe which "other part" you mean. – Xerillio Sep 26 '22 at 16:52
  • Agree we need to know the library you are using as well as sample JSON and JSON Schema -- i.e. a [mcve]. JSON Schema validation isn't built into .NET. Perhaps you are using [`SchemaExtensions.IsValid Method (JToken, JSchema, IList)`](https://www.newtonsoft.com/jsonschema/help/html/M_Newtonsoft_Json_Schema_SchemaExtensions_IsValid_2.htm) from [Json.NET Schema](https://www.newtonsoft.com/jsonschema/help/html/Introduction.htm)? – dbc Sep 26 '22 at 19:52
  • *Also this method If i change the schema always pass the json.* -- Maybe you need to set additionalProperties to false. See [Only allow properties that are declared in JSON schema](https://stackoverflow.com/q/17530762). – dbc Sep 26 '22 at 21:10

1 Answers1

0

I had similar errors with such approach.

Try add .Replace("\r", "").Replace("\n", "") at the end of code that reads data from files.

If it works - replace with less rude approach of ignoring of \r and \n.