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
@@ -29,20 +29,94 @@ it can generate a JSON document like
29
29
30
30
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.
31
31
32
+
## Use Cases {#schema-datagen-use-cases}
33
+
34
+
### Schema Debugging {#schema-datagen-debugging}
35
+
36
+
One of the more practical uses of a data generator is checking whether a schema actually says what you think it says. The generator just follows the rules, so if the output looks wrong, the schema isn't strict enough.
Suppose you want a user record that always has a `username`:
41
+
42
+
```json
43
+
{
44
+
"type": "object",
45
+
"properties": {
46
+
"username": { "type": "string" },
47
+
"email": { "type": "string", "format": "email" }
48
+
}
49
+
}
50
+
```
51
+
52
+
`properties` only describes what a property looks like _if it shows up_. It doesn't make the property show up. So the generator is perfectly happy producing:
53
+
54
+
```json
55
+
{}
56
+
```
57
+
58
+
or
59
+
60
+
```json
61
+
{ "email": "someone@example.com" }
62
+
```
63
+
64
+
Both are valid. Adding `"required": ["username"]` is what actually makes `username` mandatory, and the generator will reflect that.
*\* There are some libraries which provide limited RegEx-based string generation, but these do not support look-aheads which are required to combine multiple RegEx's with boolean logic. This functionality is required to support them alongside the aggregation keywords. I opted to just not support them at all until I can find a sufficient library.*
45
-
46
120
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.
47
121
48
122
> `$ref` support does not check for infinite loops such as occur with schemas like `{ "$ref": "#" }`. If your schema includes a reference like this, a stack overflow exception is likely.
@@ -60,14 +134,16 @@ If a format is specified, it will be used.
60
134
61
135
#### `pattern` {#schema-datagen-pattern}
62
136
63
-
Regular expressions specified via `pattern` are supported in a very limited capacity. Only simple subschemas with a single `pattern` is supported.
137
+
Regular expressions specified via `pattern` support combined constraint evaluation, including scenarios where multiple required patterns must be satisfied together.
138
+
139
+
Supported scenarios include:
64
140
65
-
-Combining multiple regular expressions using `allOf`, `anyOf`, or `oneOf` is not supported.
66
-
-Inverting regular expressions using `not` is not supported.
67
-
-Any regular expression not supported by the [FARE library](https://github.com/moodmosaic/Fare) is not supported.
68
-
-Combining `pattern`with `minLength`/`maxLength` is not supported. RegEx supports length requirements, so they should be specified within the expression.
141
+
- multiple `pattern` constraints across composed schemas
142
+
-forbidden patterns via `not`
143
+
-interactions between `pattern` and `minLength`/`maxLength`
144
+
-interactions between `pattern`and `format`
69
145
70
-
If the above scenarios are detected, a `NotSupportedException` will be thrown.
146
+
Some highly complex or mutually incompatible regex combinations may still be impossible to satisfy. In those cases, generation fails with [detailed error information](#schema-datagen-error-reporting).
71
147
72
148
### Numerics {#schema-datagen-numbers}
73
149
@@ -140,11 +216,54 @@ The result object has several properties:
140
216
-`Result` holds the value as a `JsonElement`, if successful
141
217
-`ErrorMessage` holds any error message, if unsuccessful
142
218
-`InnerResults` holds result objects from nested generations. This can be useful for debugging.
219
+
-`Location` (if available) identifies where generation failed in the target instance, as a `JsonPointer`
220
+
-`SchemaLocations` (if available) identifies one or more schema locations related to the failure, also as `JsonPointer`s
When generation fails, start with the top-level `GenerationResult` returned by `.GenerateData()`:
225
+
226
+
- If `IsSuccess` is `false`, inspect `ErrorMessage` and `InnerResults`.
227
+
-`InnerResults` contains nested failures from branches, properties, array items, or composed schemas.
228
+
- Leaf failures can provide:
229
+
-`Location` for the relative instance path that failed
230
+
-`SchemaLocations` for the schema path(s) involved in that failure
231
+
232
+
In practice, a single generation failure can contain multiple nested reasons. Walking the `InnerResults` tree is the best way to produce a full error report.
The generation isn't 100%, but most of the time it will succeed in producing a value for schemas that can have one. You may want to validate the value against the schema as a sanity check.
0 commit comments