2017-04-14 4 views
0

저는 JSON 스키마와 Json.NET 스키마가 처음이에요. 스키마 검증을 수행하는 테스트 프로그램을 작성하기 위해 샘플을 따라 가봤습니다. 임의의 스키마와 무분별한 JSON 파일을 선택했지만 끝에있는 IsValid() 호출은 True를 반환합니다. 내가 놓친 게 있니? 감사.임의의 문서에 대해 스키마 유효성 검사가 통과 되었습니까?

static void SchemaTest3() 
    { 
     string schemaJson = @"{ 
      'description': 'A person', 
      'type': 'object', 
      'properties': { 
      'name': {'type':'string'}, 
      'hobbies': { 
       'type': 'array', 
       'items': {'type':'string'} 
      } 
      } 
     }"; 
     JSchema schema = JSchema.Parse(schemaJson); 

     IList<string> errorMessages; 
     JToken jToken = JToken.Parse(@"{ 
          '@Id': 1, 
          'Email': '[email protected]', 
          'Active': true, 
          'CreatedDate': '2013-01-20T00:00:00Z', 
          'Roles': [ 
          'User', 
          'Admin' 
          ], 
          'Team': { 
          '@Id': 2, 
          'Name': 'Software Developers', 
          'Description': 'Creators of fine software products and services.' 
          } 
         }"); 
     bool isValid = jToken.IsValid(schema, out errorMessages); 
     Console.Write(isValid); 
    } 

답변

1

당신이 집어 스키마는 추가 속성을 추가 할 수 있으며 유효한 JSON은 스키마를 통과 할 이유 것을 "요구"어떤 필드를하지 않습니다.

"additionalProperties"를 추가하는 경우 : false로 설정하면 스키마가 더욱 엄격 해집니다.

http://www.jsonschemavalidator.net/을 사용하여 스키마를 재생하고 다른 옵션을 탐색 할 수 있습니다.

json 스키마로 시작하면 http://json-schema.org/examples.html이 매우 유용합니다.

더 엄격한 스키마가 있습니다.

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "description": "A person", 
    "type": "object", 
    "properties": { 
     "name": { 
      "type": "string" 
     }, 
     "hobbies": { 
      "type": "array", 
      "items": { 
       "type": "string" 
      } 
     } 
    }, 
    "additionalProperties": false 
}