2011-12-21 15 views
7

Czy walidacja schematu JSON w narzędziach common-js obsługuje odniesienia? Nie mogę tego zrobić z https://github.com/kriszyp/commonjs-utils/blob/master/json-schema.jsCzy walidacja schematu JSON w narzędziach common-js obsługuje odniesienia?

Próbowałem następujący kod:

{ 
    "type" : "object", 
    "required" : true, 
    "properties" : { 
    "id" : { 
     "type" : "number", 
     "required" : true 
    }, 
    "related" : { 
     "type" : "array", 
     "required" : true, 
     "items" : {"$ref": "$#"} 
    } 
    } 
} 

mogę śmiało przygotować schemat z obecnie odniesienia, ale byłoby miło wiedzieć, czy jest to możliwe.

Odpowiedz

19

Nie wiem jednak swoją ref wydaje się być błędne, usunąć „$” znak tj:

"items" : {"$ref": "#"} 

Oto kopia wklejony słupek Założyłem forum json dotyczące odwołań:

Zgodnie z projektem 03 tutaj jest to, co zrozumiałem o określeniu identyfikatora i odniesienie go z dowolnego miejsca. Proszę o komentarz, jeśli coś jest nie tak. Przypadki są napisane w następujący sposób: a. Uri z którego pobieram schemat b. schemat uzyskał c. jak odwoływać się do tego schematu:

1. A schema without any "id" property 
I fetch : http://someSite.com/somePath 
I get : { } 
I can ref it : {"$ref":"http://someSite.com/somePath#"} 

2. Same absolute id and uri path 
I fetch : http://someSite.com/somePath 
I get : {"id":"http://someSite.com/somePath#"} 
I can ref it : {"$ref":"http://someSite.com/somePath#"} 

3. Different absolute id and path 
I fetch : http://someSite.com/somePath 
I get : {"id":"http://anotherSite.com/anotherPath#"} 
I can ref it : {"$ref":"http://anotherSite.com/anotherPath#"} 

4. Relative - a fragment 
I fetch : http://someSite.com/somePath 
I get : {"id":"#something"} 
I can ref it : {"$ref":"http://someSite.com/somePath#something"} 

5. Relative path and fragment 
I fetch : http://someSite.com/somePath 
I get : {"id":"/oneMore/path#something"} 
I can ref it : {"$ref":"http://someSite.com/somePath/oneMore/path#something"} 

6. Relative path from file 
I fetch : file:///someFolder/someFile 
I get : {"id":"/oneMore/path#something"} 
I can ref it : {"$ref":"file:///someFolder/someFile/oneMore/path#something"} 

7. Inner schema (sub schema) with id "#subschema" (no id for the main schema) 
I fetch : http://someSite.com/somePath 
I get : {"properties" : { "aKeyName" : { "id":"#subschema" }}} 
I can ref it : {"$ref":"http://someSite.com/somePath#subschema"} 

8. Inner schema (sub schema) with id "#subschema" 
I fetch : http://someSite.com/somePath 
I get : { {"id":"#mainSchema"}, "properties" : { "aKeyName" : { "id":"#subschema" }} } 
I can ref it : {"$ref":"http://someSite.com/somePath#subschema"} 
I can also do: {"$ref":"http://someSite.com/somePath#mainSchema/properties/aKeyName"} 

9. Inner schema (within the property under the "aKeyName") but no id at all 
I fetch : http://someSite.com/somePath 
I get : { "properties" : { "aKeyName" : { }} } 
can ref it: {"$ref":"http://someSite.com/somePath#/properties/aKeyName"} 

10. Inner schema (sub schema) with a main custom absolute uri" 
I fetch : http://someSite.com/somePath 
I get : { {"id":"scheme://something"}, "properties" : { "aKeyName" : { "id":"#subschema" }} } 
I can ref it : {"$ref":"scheme://something#subschema"} 
I can also do: {"$ref":"scheme://something#/properties/aKeyName"} 

11. Inner schema with it's own absolute uri" 
I fetch : http://someSite.com/somePath 
I get : { {"id":"#main"}, "properties" : { "aKeyName" : { "id":"http://domain.com/subSchema" }} } 
I can ref it : {"$ref":"http://domain.com/subSchema#"}