You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _docs/schema/schemagen/schema-generation.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ Done.
21
21
22
22
## IMPORTANT {#schema-schemagen-disclaimer}
23
23
24
-
Ideally, this functionality should be used to create a starting point in authoring a schema. The schemas output by this library should be reviewed by actual people prior to being put into a production system.
24
+
Ideally, this functionality should be used to create a starting point in authoring a schema. The schemas output by this library should be reviewed by actual people prior to being put into a production system. This can be accomplished via approval tests or some other change review mechanism.
25
25
26
26
> The JSON Schema team generally recommends against real-time schema generation.
Copy file name to clipboardExpand all lines: _docs/schema/serialization.md
+48-5Lines changed: 48 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,8 @@ order: "01.2"
10
10
11
11
To enable this support, you'll need to include the `ValidatingJsonConverter` in the serialization options and then annotate any types that need validation with the `[JsonSchema()]` attribute, pointing the the schema for that type.
12
12
13
+
*JsonSchema.Net.Generation* also provides a version of this converter that allows the schema to be generated instead of being explicitly defined. This is probably best for most use cases, but it's a good idea to verify any generated schemas before using them in production systems.
14
+
13
15
Let's walk through it.
14
16
15
17
> More on JSON Schema support during deserialization can be found on the `json-everything`[blog](https://blog.json-everything.net/posts/deserialization-with-schemas/).
@@ -27,7 +29,32 @@ When preparing to deserialize your payload, create an options object and add the
27
29
```c#
28
30
varoptions=newJsonSerializationOptions
29
31
{
30
-
Converters= { newValidatingJsonConverter() }
32
+
Converters= { newValidatingJsonConverter
33
+
{
34
+
Options=
35
+
{
36
+
// set evaluation options
37
+
}
38
+
}
39
+
};
40
+
```
41
+
42
+
or
43
+
44
+
```c#
45
+
varoptions=newJsonSerializationOptions
46
+
{
47
+
Converters= { newValidatingJsonConverter
48
+
{
49
+
Options=
50
+
{
51
+
// set evaluation options
52
+
},
53
+
GeneratorConfiguration=
54
+
{
55
+
// set generation options
56
+
}
57
+
}
31
58
};
32
59
```
33
60
@@ -45,10 +72,10 @@ If the data isn't valid, then a `JsonException` will be thrown. The validation
> The `ValidatingJsonConverter`is a factory that creates individual typed converters and caches them. Be aware, however, that when a typed converter is used, its options are overwritten with the options you've set on the factory. This has a side effect of rendering the typed converter unsafe in multithreaded environments when using varying options. It'll be fine if you always use the same options, however.
78
+
>The `ValidatingJsonConverter` and `GenerativeValidatingJsonConverter` arefactoriesthatcreateindividualtypedconvertersandcachethem. Beaware, however, thatwhenatypedconverterisused, itsoptionsareoverwrittenwiththeoptionsyou've set on the factory. This has a side effect of rendering the typed converter unsafe in multithreaded environments when using varying options. It'llbefineifyoualwaysusethesameoptions, however.
52
79
{: .prompt-warning }
53
80
54
81
## Declaring a JSON Schema for a type {#schema-deserialization-attribute-usage}
@@ -103,7 +130,23 @@ public class MyModel
103
130
}
104
131
```
105
132
106
-
That's it.
133
+
## Generating a JSON Schema for a type {#schema-deserialization-generation}
Thishasthesameoutcomeascreatingtheexplicitschemafromtheprevioussection. However, it's important to remember that writing your schema explicitly will always be more flexible that generation. If the schemas you want have fairly complex logic, perhaps explicitly writing your schemas is the right approach for you.
107
150
108
151
## Why not use `System.ComponentModel.DataAnnotations` to annotate and validate the model? {#schema-deserialization-why}
0 commit comments