Skip to content

Commit 743d3d5

Browse files
committed
add generative validation json conversion
1 parent 2c33327 commit 743d3d5

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

_docs/schema/schemagen/schema-generation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Done.
2121

2222
## IMPORTANT {#schema-schemagen-disclaimer}
2323

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.
2525

2626
> The JSON Schema team generally recommends against real-time schema generation.
2727
{: .prompt-danger }

_docs/schema/serialization.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ order: "01.2"
1010

1111
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.
1212

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+
1315
Let's walk through it.
1416

1517
> 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
2729
```c#
2830
var options = new JsonSerializationOptions
2931
{
30-
Converters = { new ValidatingJsonConverter() }
32+
Converters = { new ValidatingJsonConverter
33+
{
34+
Options =
35+
{
36+
// set evaluation options
37+
}
38+
}
39+
};
40+
```
41+
42+
or
43+
44+
```c#
45+
var options = new JsonSerializationOptions
46+
{
47+
Converters = { new ValidatingJsonConverter
48+
{
49+
Options =
50+
{
51+
// set evaluation options
52+
},
53+
GeneratorConfiguration =
54+
{
55+
// set generation options
56+
}
57+
}
3158
};
3259
```
3360

@@ -45,10 +72,10 @@ If the data isn't valid, then a `JsonException` will be thrown. The validation
4572

4673
The validation can be configured using properties on the converter.
4774

48-
- `OutputFormat` configures the JSON Schema output format to be used. By default, this value is `Flag` (the same as on `EvaluationOptions`).
49-
- `RequireFormatValidation` will validate the `format` keyword when set to true. By default `format` is an annotation.
75+
- `Options`, which is available on both converters, configures schema evaluations.
76+
- `GeneratorConfiguration`, which is available on the generative version only, configures schema generation.
5077

51-
> 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` are factories that create individual typed converters and cache 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.
5279
{: .prompt-warning }
5380

5481
## Declaring a JSON Schema for a type {#schema-deserialization-attribute-usage}
@@ -103,7 +130,23 @@ public class MyModel
103130
}
104131
```
105132

106-
That's it.
133+
## Generating a JSON Schema for a type {#schema-deserialization-generation}
134+
135+
To do the same kind of validation for the generative case, add the `[GenerateJsonSchema]` attribute and any applicable schema attributes. You can find out more about schema generation attributes [here](./schemagen/schema-generation).
136+
137+
```c#
138+
[GenerateJsonSchema]
139+
[AdditionalProperties(false)]
140+
public class MyModel
141+
{
142+
[MinLength(10)]
143+
[MaxLength(50)]
144+
[Required]
145+
public string Foo { get; set; }
146+
}
147+
```
148+
149+
This has the same outcome as creating the explicit schema from the previous section. 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.
107150

108151
## Why not use `System.ComponentModel.DataAnnotations` to annotate and validate the model? {#schema-deserialization-why}
109152

0 commit comments

Comments
 (0)