11using System ;
22using System . Text . Json ;
33using System . Text . Json . Nodes ;
4+ using FluentAssertions ;
45using NUnit . Framework ;
56
6- namespace Json . Schema . Tests
7+ namespace Json . Schema . Tests ;
8+
9+ public class BaseDocumentTests
710{
8- public class BaseDocumentTests
11+ [ Test ]
12+ public void SchemasEmbeddedInJsonCanBeReferenced_Valid ( )
913 {
10- [ Test ]
11- public void SchemasEmbeddedInJsonCanBeReferenced_Valid ( )
14+ JsonSchema targetSchema = new JsonSchemaBuilder ( )
15+ . Type ( SchemaValueType . Integer ) ;
16+
17+ var json = new JsonObject
18+ {
19+ [ "prop1" ] = "foo" ,
20+ [ "prop2" ] = new JsonArray
21+ {
22+ "bar" ,
23+ JsonSerializer . SerializeToNode ( targetSchema )
24+ }
25+ } ;
26+
27+ var options = new EvaluationOptions
1228 {
13- JsonSchema targetSchema = new JsonSchemaBuilder ( )
14- . Type ( SchemaValueType . Integer ) ;
29+ OutputFormat = OutputFormat . List
30+ } ;
31+
32+ var jsonBaseDoc = new JsonNodeBaseDocument ( json , new Uri ( "http://localhost:1234/doc" ) ) ;
33+ options . SchemaRegistry . Register ( jsonBaseDoc ) ;
34+
35+ JsonSchema subjectSchema = new JsonSchemaBuilder ( )
36+ . Ref ( "http://localhost:1234/doc#/prop2/1" ) ;
37+
38+ JsonNode instance = 42 ;
39+
40+ var result = subjectSchema . Evaluate ( instance , options ) ;
41+
42+ result . AssertValid ( ) ;
43+ }
44+
45+ [ Test ]
46+ public void SchemasEmbeddedInJsonCanBeReferenced_Invalid ( )
47+ {
48+ JsonSchema targetSchema = new JsonSchemaBuilder ( )
49+ . Type ( SchemaValueType . Integer ) ;
50+
51+ var json = new JsonObject
52+ {
53+ [ "prop1" ] = "foo" ,
54+ [ "prop2" ] = new JsonArray
55+ {
56+ "bar" ,
57+ JsonSerializer . SerializeToNode ( targetSchema )
58+ }
59+ } ;
60+
61+ var options = new EvaluationOptions
62+ {
63+ OutputFormat = OutputFormat . List
64+ } ;
65+
66+ var jsonBaseDoc = new JsonNodeBaseDocument ( json , new Uri ( "http://localhost:1234/doc" ) ) ;
67+ options . SchemaRegistry . Register ( jsonBaseDoc ) ;
1568
16- var json = new JsonObject
69+ JsonSchema subjectSchema = new JsonSchemaBuilder ( )
70+ . Ref ( "http://localhost:1234/doc#/prop2/1" ) ;
71+
72+ JsonNode instance = "baz" ! ;
73+
74+ var result = subjectSchema . Evaluate ( instance , options ) ;
75+
76+ result . AssertInvalid ( ) ;
77+ }
78+
79+ [ Test ]
80+ public void ReferencesFromWithinEmbeddedSchemas ( )
81+ {
82+ var json = new JsonObject
83+ {
84+ [ "prop1" ] = "foo" ,
85+ [ "prop2" ] = new JsonArray
1786 {
18- [ "prop1" ] = "foo ",
19- [ "prop2" ] = new JsonArray
87+ "bar ",
88+ new JsonObject
2089 {
21- "bar" ,
22- JsonSerializer . SerializeToNode ( targetSchema )
90+ [ "type" ] = "integer"
2391 }
24- } ;
25-
26- var options = new EvaluationOptions
92+ } ,
93+ [ "prop3" ] = new JsonObject
2794 {
28- OutputFormat = OutputFormat . List
29- } ;
95+ [ "$ref" ] = "#/prop2/1"
96+ }
97+ } ;
3098
31- var jsonBaseDoc = new JsonNodeBaseDocument ( json , new Uri ( "http://localhost:1234/doc" ) ) ;
32- options . SchemaRegistry . Register ( jsonBaseDoc ) ;
99+ var options = new EvaluationOptions
100+ {
101+ OutputFormat = OutputFormat . List
102+ } ;
33103
34- JsonSchema subjectSchema = new JsonSchemaBuilder ( )
35- . Ref ( "http://localhost:1234/doc#/prop2/1" ) ;
104+ var jsonBaseDoc = new JsonNodeBaseDocument ( json , new Uri ( "http://localhost:1234/doc" ) ) ;
105+ options . SchemaRegistry . Register ( jsonBaseDoc ) ;
36106
37- JsonNode instance = 42 ;
107+ JsonSchema subjectSchema = new JsonSchemaBuilder ( )
108+ . Ref ( "http://localhost:1234/doc#/prop3" ) ;
38109
39- var result = subjectSchema . Evaluate ( instance , options ) ;
110+ JsonNode instance = 42 ;
40111
41- result . AssertValid ( ) ;
42- }
112+ var result = subjectSchema . Evaluate ( instance , options ) ;
43113
44- [ Test ]
45- public void SchemasEmbeddedInJsonCanBeReferenced_Invalid ( )
46- {
47- JsonSchema targetSchema = new JsonSchemaBuilder ( )
48- . Type ( SchemaValueType . Integer ) ;
114+ result . IsValid . Should ( ) . BeTrue ( ) ;
115+ }
49116
50- var json = new JsonObject
117+ [ Test ]
118+ public void NestedReferencesFromWithinEmbeddedSchemas ( )
119+ {
120+ var json = new JsonObject
121+ {
122+ [ "prop1" ] = "foo" ,
123+ [ "prop2" ] = new JsonArray
51124 {
52- [ "prop1" ] = "foo ",
53- [ "prop2" ] = new JsonArray
125+ "bar ",
126+ new JsonObject
54127 {
55- "bar" ,
56- JsonSerializer . SerializeToNode ( targetSchema )
128+ [ "type" ] = "integer"
57129 }
58- } ;
59-
60- var options = new EvaluationOptions
130+ } ,
131+ [ "prop3" ] = new JsonObject
61132 {
62- OutputFormat = OutputFormat . List
63- } ;
133+ [ "properties" ] = new JsonObject
134+ {
135+ [ "data" ] = new JsonObject
136+ {
137+ [ "$ref" ] = "#/prop2/1"
138+ }
139+
140+ }
141+ }
142+ } ;
143+
144+ var options = new EvaluationOptions
145+ {
146+ OutputFormat = OutputFormat . List
147+ } ;
64148
65- var jsonBaseDoc = new JsonNodeBaseDocument ( json , new Uri ( "http://localhost:1234/doc" ) ) ;
66- options . SchemaRegistry . Register ( jsonBaseDoc ) ;
149+ var jsonBaseDoc = new JsonNodeBaseDocument ( json , new Uri ( "http://localhost:1234/doc" ) ) ;
150+ options . SchemaRegistry . Register ( jsonBaseDoc ) ;
67151
68- JsonSchema subjectSchema = new JsonSchemaBuilder ( )
69- . Ref ( "http://localhost:1234/doc#/prop2/1 " ) ;
152+ JsonSchema subjectSchema = new JsonSchemaBuilder ( )
153+ . Ref ( "http://localhost:1234/doc#/prop3 " ) ;
70154
71- JsonNode instance = "baz" ! ;
155+ JsonNode instance = new JsonObject { [ "data" ] = 42 } ;
72156
73- var result = subjectSchema . Evaluate ( instance , options ) ;
157+ var result = subjectSchema . Evaluate ( instance , options ) ;
74158
75- result . AssertInvalid ( ) ;
76- }
159+ result . IsValid . Should ( ) . BeTrue ( ) ;
77160 }
78- }
161+ }
0 commit comments