|
336 | 336 | - instance: 6.8
|
337 | 337 | format: date-time
|
338 | 338 | isValid: false
|
| 339 | +- id: 26b6ebca-58e6-4814-8dea-4946d444c9a8 |
| 340 | + background: | |
| 341 | + _JsonSchema.Net_ supports every version of JSON Schema from Draft 6 forward. |
| 342 | +
|
| 343 | + Version selection can be key to figuring out how to evaluate a schema. Generally, |
| 344 | + the version will be determined by the meta-schema, as declared by the `$schema` |
| 345 | + keyword. However, when `$schema` is absent, it's up to the tool and user to |
| 346 | + decide. |
| 347 | +
|
| 348 | + To set the JSON Schema version to use, you'll need to set the `EvaluateAs` property |
| 349 | + on the evaluation options. |
| 350 | +
|
| 351 | + This setting will have no effect if the schema _does_ have `$schema`. |
| 352 | +
|
| 353 | + Lastly, this property is init-only, so you'll need to set it when creating the |
| 354 | + options object. |
| 355 | +
|
| 356 | + \* _This lesson works by taking advantage of the fact that JSON Schema ignores unknown |
| 357 | + keywords. So when it evaluates a schema as Draft 6 that contains keywords from |
| 358 | + Draft 7, the Draft 7 keywords aren't processed._ |
| 359 | + docs: 'schema/basics/#schema-options' |
| 360 | + title: 'Options: JSON Schema Version' |
| 361 | + instructions: | |
| 362 | + Create and configure an evaluation options object, and set the JSON Schema version |
| 363 | + found in the `schemaVersion` variable. |
| 364 | + inputTemplate: '' |
| 365 | + contextCode: |- |
| 366 | + using System; |
| 367 | + using System.Collections.Generic; |
| 368 | + using System.Text.Json; |
| 369 | + using System.Text.Json.Nodes; |
| 370 | + using System.Text.Json.Serialization; |
| 371 | + using Json.Schema; |
| 372 | + using Json.More; |
| 373 | + |
| 374 | + namespace LearnJsonEverything; |
| 375 | + |
| 376 | + public class Lesson : ILessonRunner<EvaluationResults> |
| 377 | + { |
| 378 | + public EvaluationResults Run(JsonObject test) |
| 379 | + { |
| 380 | + var instance = test["instance"]; |
| 381 | + var specVersion = test["version"].Deserialize<SpecVersion>( |
| 382 | + new JsonSerializerOptions |
| 383 | + { |
| 384 | + Converters = { new EnumStringConverter<SpecVersion>() } |
| 385 | + } |
| 386 | + ); |
| 387 | +
|
| 388 | + JsonSchema schema = new JsonSchemaBuilder() |
| 389 | + .Type(SchemaValueType.Array) |
| 390 | + .Items(new JsonSchemaBuilder() |
| 391 | + .Type(SchemaValueType.Integer) |
| 392 | + ) |
| 393 | + .Contains(new JsonSchemaBuilder().Const(4)) |
| 394 | + // Introduced with Draft 7 |
| 395 | + .If(new JsonSchemaBuilder().MinItems(3)) |
| 396 | + .Then(new JsonSchemaBuilder() |
| 397 | + .Items(new JsonSchemaBuilder().MultipleOf(2)) |
| 398 | + ) |
| 399 | + // Introduced with Draft 2019-09 |
| 400 | + .MinContains(2) |
| 401 | + // Introduced with Draft 2020-12 |
| 402 | + .PrefixItems( |
| 403 | + new JsonSchemaBuilder().Type(SchemaValueType.String), |
| 404 | + new JsonSchemaBuilder().Type(SchemaValueType.Boolean) |
| 405 | + ); |
| 406 | +
|
| 407 | + /* USER CODE */ |
| 408 | +
|
| 409 | + return schema.Evaluate(instance, options); |
| 410 | + } |
| 411 | + } |
| 412 | + tests: |
| 413 | + - instance: [{},4] |
| 414 | + version: Draft6 |
| 415 | + isValid: false |
| 416 | + - instance: [2,3,4,5] |
| 417 | + version: Draft6 |
| 418 | + isValid: true |
| 419 | + - instance: [2,3,4,5] |
| 420 | + version: Draft7 |
| 421 | + isValid: false |
| 422 | + - instance: [2,4,6,8] |
| 423 | + version: Draft7 |
| 424 | + isValid: true |
| 425 | + - instance: [2,4,6,8] |
| 426 | + version: Draft201909 |
| 427 | + isValid: false |
| 428 | + - instance: [2,4,6,8,4] |
| 429 | + version: Draft201909 |
| 430 | + isValid: true |
| 431 | + - instance: [2,4,6,8,4] |
| 432 | + version: Draft202012 |
| 433 | + isValid: false |
| 434 | + - instance: ["a string",true,2,4,6,8,4] |
| 435 | + version: Draft202012 |
| 436 | + isValid: true |
0 commit comments