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: json-everything.net/wwwroot/md/schema-basics.md
+24-24Lines changed: 24 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
-
# Overview
1
+
# Overview {#schema-basics}
2
2
3
3
The occasion may arise when you wish to validate that a JSON object is in the correct form (has the appropriate keys and the right types of values), or perhaps you wish to annotate that data. Enter JSON Schema. Much like XML Schema with XML, JSON Schema defines a pattern for JSON data. A JSON Schema validator can verify that a given JSON object meets the requirements as defined by the JSON Schema as well as provide additional information to the application about the data. This evaluation can come in handy as a precursor step before deserializing.
4
4
5
5
More information about JSON Schema can be found at [json-schema.org](http://json-schema.org).
6
6
7
7
To support JSON Schema, JsonSchema.Net exposes the `JsonSchema` type. This type is implemented as a list of keywords, each of which correspond to one of the keywords defined in the JSON Schema specifications.
8
8
9
-
## Specification versions
9
+
## Specification versions {#schema-versions}
10
10
11
11
There are currently six drafts of the JSON Schema specification that have known use:
12
12
@@ -21,23 +21,23 @@ JsonSchema.Net supports draft 6 and later.
21
21
22
22
The next version, which will be supported by v4.0.0 and later of this library, is currently in development and will start a new era for the project which includes various backward- and forward-compatibility guarantees. Have a read of the various discussions happening in the [JSON Schema GitHub org](https://github.com/json-schema-org) for more information.
23
23
24
-
### Meta-schemas
24
+
### Meta-schemas {#schema-metaschemas}
25
25
26
26
Each version defines a meta-schema. This is a special JSON Schema that describes all of the keywords available for that version. They are intended to be used to validate other schemas. Usually, a schema will declare the version it should adhere to using the `$schema` keyword.
27
27
28
28
JsonSchema.Net declares the meta-schemas for the supported versions as members of the `MetaSchemas` static class.
29
29
30
30
Draft 2019-09 introduced vocabularies. As part of this new feature, the meta-schemas for this version and those which follow it have been split into vocabulary-specific meta-schemas. Additionally, the specification recognizes that the meta-schemas aren't perfect and may need to be updated occasionally. As such, the meta-schemas defined by this library will be updated to match, in most cases only triggering a patch release.
31
31
32
-
## Keywords
32
+
## Keywords {#schema-keywords}
33
33
34
34
JSON Schema is expressed as a collection of keywords, each of which provides a specific constraint on a JSON instance. For example, the `type` keyword specifies what JSON type an instance may be, whereas the `minimum` keyword specifies a minimum numeric value *for only numeric data* (it will not apply any assertion to non-numeric values). These keywords can be combined to express the expected shape of any JSON instance. Once defined, the schema evaluates the instance, providing feedback on what errors occurred, including where in the instance and in the schema produced them.
35
35
36
-
# Building a schema
36
+
# Building a schema {#schema-build}
37
37
38
38
There are two options when building a schema: defining it inline using the fluent builder and defining it externally and deserializing. Which method you use depends on your specific requirements.
39
39
40
-
## Deserialization
40
+
## Deserialization {#schema-deserialization}
41
41
42
42
JsonSchema.Net schemas are fully serializable.
43
43
@@ -53,7 +53,7 @@ var mySchema = JsonSerializer.Deserialize<JsonSchema>(content);
53
53
54
54
Done.
55
55
56
-
## Inline
56
+
## Inline {#schema-inlining}
57
57
58
58
There are many reasons why you would want to hard-code your schemas. This library actually hard-codes all of the meta-schemas. Whatever your reason, the `JsonSchemaBuilder` class is going to be your friend.
59
59
@@ -70,7 +70,7 @@ var schema = builder.Build();
70
70
71
71
Let's take a look at some of the builder extension methods.
72
72
73
-
### Easy mode
73
+
### Easy mode {#schema-how-to-1}
74
74
75
75
Some of the more straightforward builder methods are for like the `title` and `$comment` keywords, which just take a string:
76
76
@@ -81,7 +81,7 @@ builder.Comment("a comment")
81
81
82
82
Notice that these methods implement a fluent interface so that you can chain them together.
83
83
84
-
### A little spice
84
+
### A little spice {#schema-how-to-2}
85
85
86
86
Other extension methods can take multiple values. These have been overloaded to accept both `IEnumerable<T>` and `params` arrays just to keep things flexible.
87
87
@@ -96,7 +96,7 @@ or just
96
96
builder.Required("prop1", "prop2");
97
97
```
98
98
99
-
### Now you're cooking with gas
99
+
### Now you're cooking with gas {#schema-how-to-3}
100
100
101
101
Lastly, we have the extension methods which take advantage of C# 7 tuples. These include keywords like `$defs` and `properties` which take objects to mimic their JSON form.
102
102
@@ -115,7 +115,7 @@ builder.Properties(
115
115
116
116
Did you notice how the `JsonSchemaBuilder` is just included directly without the `.Build()` method? These methods actually require `JsonSchema` objects. This leads us into the next part.
117
117
118
-
### Conversions
118
+
### Conversions {#schema-implicit-cast}
119
119
120
120
`JsonSchemaBuilder` defines an implicit cast to `JsonSchema` which calls the `.Build()` method.
121
121
@@ -156,11 +156,11 @@ builder.Properties(
156
156
.AdditionalProperties(false);
157
157
```
158
158
159
-
# Evaluation & annotations
159
+
# Evaluation & annotations {#schema-evaluation}
160
160
161
161
***NOTE** In recognizing the multitude of uses for JSON Schema, the team has started to use the word "evaluate" instead of "validate" for the general processing of a schema. What was "validate" in v3.x of this library is now "evaluate" in order to align with this viewpoint.*
162
162
163
-
## Evaluating instances
163
+
## Evaluating instances {#schema-evaluation-2}
164
164
165
165
`JsonSchema` exposes an `Evaluate()` method which is used to evaluate JSON instances. Let's begin with the following schema and a few JSON objects:
166
166
@@ -224,7 +224,7 @@ In the above example, the following would result:
224
224
225
225
No errors would actually be reported here because the output format defaults to a "flag" format, which is a basic pass/fail. To get specific errors, the output format will need to be configured.
226
226
227
-
## Evaluation results
227
+
## Evaluation results {#schema-results}
228
228
229
229
JSON Schema draft 2019-09 began the process to standardize the format for evaluation output in order to support cross-platform and cross-implementation compatibility. The format has been updated for the upcoming release to be more concise and clear. This includes support for both errors and annotation collection.
230
230
@@ -240,7 +240,7 @@ The default output format is Flag, but this can be configured via the `Evaluatio
240
240
241
241
***NOTE** It's only possible to translate from a more detailed to a less detailed format.*
242
242
243
-
### Examples of output
243
+
### Examples of output {#schema-output}
244
244
245
245
<details>
246
246
<summary>Hierarchical</summary>
@@ -377,7 +377,7 @@ The default output format is Flag, but this can be configured via the `Evaluatio
377
377
378
378
</details>
379
379
380
-
## Value format validation
380
+
## Value format validation {#schema-format}
381
381
382
382
The `format` keyword has been around a while. It's available in all of the versions supported by JsonSchema.Net. Although this keyword is technically classified as an annotation, the specification does allow (the word used is "SHOULD") that implementation provide some level of validation on it so long as that validation may be configured on and off.
383
383
@@ -405,7 +405,7 @@ New formats must be registered via the `Formats.Register()` static method. This
405
405
406
406
***IMPORTANT** Format implementations MUST not contain state as the same instance will be shared by all of the schema instances that use it.
407
407
408
-
## Options
408
+
## Options {#schema-options}
409
409
410
410
The `EvaluationOptions` class gives you a few configuration points for customizing how the evaluation process behaves. It is an instance class and can be passed into the `JsonSchema.Evaluate()` method. If no options are explicitly passed, a copy of `JsonSchemaOptions.Default` will be used.
411
411
@@ -416,11 +416,11 @@ The `EvaluationOptions` class gives you a few configuration points for customizi
416
416
417
417
_\* If you're using a custom meta-schema, you'll need to load it per the [Schema Registration](json-schema#schema-registration) section below. Custom meta-schemas form a chain of meta-schemas (e.g. your custom meta-schema may reference another which references the draft 2020-12 meta-schema). Ultimately, the chain MUST end at a JSON-Schema-defined meta-schema as this defines the processing rules for the schema. An error will be produced if the meta-schema chain ends at a meta-schema that is unrecognized._
418
418
419
-
# Managing references (`$ref`)
419
+
# Managing references (`$ref`) {#schema-ref}
420
420
421
421
By default, JsonSchema.Net handles all references as defined in the draft 2020-12 version of the JSON Schema specification. What this means for draft 2019-09 and later schemas is that `$ref` can now exist alongside other keywords; for earlier versions (i.e. Drafts 6 and 7), keywords as siblings to `$ref` will be ignored.
422
422
423
-
## Schema resolution
423
+
## Schema resolution {#schema-ref-resolution}
424
424
425
425
In order to resolve references more quickly, JsonSchema.Net maintains two registries for all schemas and identifiable subschemas that it has encountered. The first is a global registry, and the second is a local registry that is passed around on the evaluation context. If a schema is not found in the local registry, it will automatically fall back to the global registry.
In addition to schemas, other identifiable documents can be registered. For example, Open API documents _contain_ schemas but are not themselves schemas. Additionally, references between schemas within these documents are relative to the document root. Registering the Open API document will allow these references to be resolved.
468
468
@@ -486,17 +486,17 @@ var schema = new JsonSchemaBuilder()
Copy file name to clipboardExpand all lines: json-everything.net/wwwroot/md/schema-datagen.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Generation Sample JSON Data from a Schema
1
+
# Generation Sample JSON Data from a Schema {#schema-datagen}
2
2
3
3
JsonSchema.Net.DataGeneration is a tool that can create JSON data instances using a JSON schema as a framework.
4
4
@@ -23,7 +23,7 @@ it can generate a JSON document like
23
23
24
24
Under the covers, the library uses the fabulous [Bogus](https://github.com/bchavez/Bogus) library, which is commonly used to generate random test data, and a few other tricks.
25
25
26
-
## Capabilities
26
+
## Capabilities {#schema-datagen-capabilities}
27
27
28
28
This library is quite powerful. It supports most JSON Schema keywords, including `if`/`then`/`else` and aggregation keywords (`oneOf`, `allOf`, etc.).
29
29
@@ -39,15 +39,15 @@ It currently does not support:
39
39
40
40
Everything else _should_ be mostly supported. Feel free to [open an issue](https://github.com/gregsdennis/json-everything/issues/new/choose) if you find something isn't working as you expect.
41
41
42
-
### Strings & `format`
42
+
### Strings & `format` {#schema-datagen-strings}
43
43
44
44
All of the formats listed in the draft 2020-12 specification are supported, at least to the extent that they can be validated by JsonSchema.Net.
45
45
46
46
If a format is specified, it will be used.
47
47
48
48
If a format is not specified, Bogus's Lorem Ipsum generator is used to create some nice (but oddly readable) garbage text.
49
49
50
-
### Numerics
50
+
### Numerics {#schema-datagen-numbers}
51
51
52
52
Integer and number generation each have custom algorithms that produce values that align with minimums, maximums, multiples, and even anti-multiples (numbers that should _not_ be divisors).
53
53
@@ -81,7 +81,7 @@ the only valid integers are
81
81
82
82
The library will generate such values with ease.
83
83
84
-
### Arrays & Objects
84
+
### Arrays & Objects {#schema-datagen-structured}
85
85
86
86
Care needs to be taken when specifying arrays that can have additional items or objects that can have additional properties. This library will unsubtly create moderatly deep trees of data if allowed.
87
87
@@ -100,7 +100,7 @@ To combat this, there are some built-in limitations:
100
100
- Item and property counts default to 0-10.
101
101
- Arrays and objects have a lower chance of generating than the simpler types (null, integer, number, string).
102
102
103
-
# Generating Data
103
+
# Generating Data {#schema-datagen-generation}
104
104
105
105
All you need to generate data is a schema object. This can be built inline or read in from an external source. The instructions for that are on the "Overview" tab.
106
106
@@ -119,7 +119,7 @@ The result object has several properties:
119
119
-`ErrorMessage` holds any error message, if unsuccessful
120
120
-`InnerResults` holds result objects from nested generations. This can be useful for debugging.
0 commit comments