diff --git a/_framework/LearnJsonEverything.Template.dll b/_framework/LearnJsonEverything.Template.dll index 1a12929..155112c 100644 Binary files a/_framework/LearnJsonEverything.Template.dll and b/_framework/LearnJsonEverything.Template.dll differ diff --git a/_framework/LearnJsonEverything.Template.dll.br b/_framework/LearnJsonEverything.Template.dll.br index 3200e54..270396c 100644 Binary files a/_framework/LearnJsonEverything.Template.dll.br and b/_framework/LearnJsonEverything.Template.dll.br differ diff --git a/_framework/LearnJsonEverything.Template.dll.gz b/_framework/LearnJsonEverything.Template.dll.gz index 86191ae..47c1846 100644 Binary files a/_framework/LearnJsonEverything.Template.dll.gz and b/_framework/LearnJsonEverything.Template.dll.gz differ diff --git a/_framework/LearnJsonEverything.Template.pdb.gz b/_framework/LearnJsonEverything.Template.pdb.gz index 8336592..6a273ce 100644 Binary files a/_framework/LearnJsonEverything.Template.pdb.gz and b/_framework/LearnJsonEverything.Template.pdb.gz differ diff --git a/_framework/LearnJsonEverything.dll b/_framework/LearnJsonEverything.dll index 0483f62..3f4e2b4 100644 Binary files a/_framework/LearnJsonEverything.dll and b/_framework/LearnJsonEverything.dll differ diff --git a/_framework/LearnJsonEverything.dll.br b/_framework/LearnJsonEverything.dll.br index 04874c6..fcad48b 100644 Binary files a/_framework/LearnJsonEverything.dll.br and b/_framework/LearnJsonEverything.dll.br differ diff --git a/_framework/LearnJsonEverything.dll.gz b/_framework/LearnJsonEverything.dll.gz index 2f449dd..2045b94 100644 Binary files a/_framework/LearnJsonEverything.dll.gz and b/_framework/LearnJsonEverything.dll.gz differ diff --git a/_framework/LearnJsonEverything.pdb.gz b/_framework/LearnJsonEverything.pdb.gz index 8c655f9..830e63f 100644 Binary files a/_framework/LearnJsonEverything.pdb.gz and b/_framework/LearnJsonEverything.pdb.gz differ diff --git a/_framework/blazor.boot.json b/_framework/blazor.boot.json index 342c333..381395d 100644 --- a/_framework/blazor.boot.json +++ b/_framework/blazor.boot.json @@ -1,7 +1,7 @@ { "mainAssemblyName": "LearnJsonEverything", "resources": { - "hash": "sha256-/mfCyRbUz0PTj+9KQsEFe06GWi15knYDk0T/2h0FB+4=", + "hash": "sha256-we72luh3SW/LOe3cXuF/tn/b2V24P9/Z9Kd5nwZnJqs=", "jsModuleNative": { "dotnet.native.8.0.6.suuomc2hr0.js": "sha256-YU6M9+jtDVnB8/wtrgIndOstzNh778Ls8g9XnbjorSk=" }, @@ -24,8 +24,8 @@ "Json.More.dll": "sha256-xr0aE0+HsrvtjruJdaapKt2/DGB0qSUz+xD0XmvawxM=", "JsonPointer.Net.dll": "sha256-DzV9BEIGNC1mcOpb+JYI2gGXuDG5qg157LjFTnJ6pAE=", "JsonSchema.Net.dll": "sha256-iCLcArFhOdHD16SQ2km8JXYsJmbv4fa37t+iopIb8+w=", - "LearnJsonEverything.dll": "sha256-HMhC+Eb7AO6iyR3thMWDV60cIw9GaIXEmt5K87t1CUQ=", - "LearnJsonEverything.Template.dll": "sha256-NqX+HByG0vRSrwzta/CYol2kSbSCsGKVammRUbswD24=", + "LearnJsonEverything.dll": "sha256-aczvZo8vmBSgeUl7MHBCHcnm3m0OoDx8OovLP0yzCNA=", + "LearnJsonEverything.Template.dll": "sha256-aHRf87hKDnj5TSiRZDRGWixD5dGPi6qCQ+aHxTy56dM=", "Markdig.dll": "sha256-34Ry04hh+o21Q3VDOJoD0lmCasZYDBsC9CvOeWz3BjI=", "Markdig.SyntaxHighlighting.dll": "sha256-YEfO9OsS0T4kDjPXOW2pJF8qg62omn4UYMy/C2tcmC4=", "Microsoft.AspNetCore.Authorization.dll": "sha256-IsV1p8+7qyVgHgqn8Yon3RZfqKDNjdC/kTwzUwtaAlY=", diff --git a/_framework/blazor.boot.json.br b/_framework/blazor.boot.json.br index e6820b4..28f9b60 100644 Binary files a/_framework/blazor.boot.json.br and b/_framework/blazor.boot.json.br differ diff --git a/_framework/blazor.boot.json.gz b/_framework/blazor.boot.json.gz index a9dd908..12cbe74 100644 Binary files a/_framework/blazor.boot.json.gz and b/_framework/blazor.boot.json.gz differ diff --git a/css/app.css b/css/app.css index 5c86a2e..937fd48 100644 --- a/css/app.css +++ b/css/app.css @@ -95,6 +95,10 @@ a, border-style: solid; } +.monaco-editor { + position: absolute !important; +} + #blazor-error-ui { background-color: #1e1e1e; bottom: 0; diff --git a/data/lessons/schema.yaml b/data/lessons/schema.yaml index 0b55c5f..795627e 100644 --- a/data/lessons/schema.yaml +++ b/data/lessons/schema.yaml @@ -1,4 +1,120 @@ --- +- id: 26b6ebca-58e6-4814-86ea-4946d844c9a8 + background: | + JSON Schema is typically itself represented in JSON. To support this, the `JsonSchema` + type is completely compatible with the _System.Text.Json_ serializer. + docs: 'schema/basics/#schema-deserialization' + title: Deserializing a schema + instructions: | + Deserialize the text in `schemaText` into a `JsonSchema` variable called `schema`. + inputTemplate: '' + contextCode: |- + using System; + using System.Collections.Generic; + using System.Text.Json; + using System.Text.Json.Nodes; + using System.Text.Json.Serialization; + using Json.Schema; + + namespace LearnJsonEverything; + + public class Lesson : ILessonRunner + { + public EvaluationResults Run(JsonObject test) + { + var instance = test["instance"]; + var schemaText = + """ + { + "type": "object", + "properties": { + "foo": { "type": "number", "minimum": 0 }, + "bar": { "type": "string" } + }, + "required": ["foo", "bar"] + } + """; + + /* USER CODE */ + + return schema.Evaluate(instance); + } + } + tests: + - instance: { "foo": 13, "bar": "a string" } + isValid: true + - instance: { "foo": false, "bar": "a string" } + isValid: false + - instance: { "foo": 13 } + isValid: false + - instance: { "bar": "a string" } + isValid: false + - instance: [1,2,3] + isValid: false + - instance: 6.8 + isValid: false +- id: 26b6ebca-58e6-4824-86ea-4946d844c9a8 + skip: true + background: | + JSON Schema is typically itself represented in JSON. To support this, the `JsonSchema` + type is completely compatible with the _System.Text.Json_ serializer. + + When compiling a Native AOT application or when trimming assemblies, you'll need to add + `JsonSchema` and `EvaluationOptions` to your serializer context in order to support + source generation. + docs: 'schema/basics/#aot' + title: Deserializing a schema (AOT) + instructions: | + Create a serializer context class called `MySerializerContext` and add the appropriate + attributes. + inputTemplate: '' + contextCode: |- + using System; + using System.Collections.Generic; + using System.Text.Json; + using System.Text.Json.Nodes; + using System.Text.Json.Serialization; + using Json.Schema; + + namespace LearnJsonEverything; + + public class Lesson : ILessonRunner + { + public EvaluationResults Run(JsonObject test) + { + var instance = test["instance"]; + var schemaText = + """ + { + "type": "object", + "properties": { + "foo": { "type": "number", "minimum": 0 }, + "bar": { "type": "string" } + }, + "required": ["foo", "bar"] + } + """; + + JsonSerializer.Deserialize(schemaText, MySerializerContext.Default.JsonSchema); + + return schema.Evaluate(instance); + } + } + + /* USER CODE */ + tests: + - instance: { "foo": 13, "bar": "a string" } + isValid: true + - instance: { "foo": false, "bar": "a string" } + isValid: false + - instance: { "foo": 13 } + isValid: false + - instance: { "bar": "a string" } + isValid: false + - instance: [1,2,3] + isValid: false + - instance: 6.8 + isValid: false - id: 26b6ebca-58e6-4814-86ea-4946d844c9a6 title: 'Schema Builder: Any string' background: | @@ -21,9 +137,9 @@ public class Lesson : ILessonRunner { - public EvaluationResults Run(JsonObject context) + public EvaluationResults Run(JsonObject test) { - var instance = context["instance"]; + var instance = test["instance"]; var builder = new JsonSchemaBuilder(); /* USER CODE */ @@ -64,9 +180,9 @@ public class Lesson : ILessonRunner { - public EvaluationResults Run(JsonObject context) + public EvaluationResults Run(JsonObject test) { - var instance = context["instance"]; + var instance = test["instance"]; var builder = new JsonSchemaBuilder(); /* USER CODE */ @@ -123,9 +239,9 @@ public class Lesson : ILessonRunner { - public EvaluationResults Run(JsonObject context) + public EvaluationResults Run(JsonObject test) { - var instance = context["instance"]; + var instance = test["instance"]; var builder = new JsonSchemaBuilder(); /* USER CODE */ @@ -151,14 +267,16 @@ isValid: false - instance: false isValid: false -- id: 26b6ebca-58e6-4814-86ea-4946d844c9a8 +- id: 26b6ebca-58e6-4814-86ea-4946d444c9a8 background: | - The `JsonSchemaBuilder` class provides a type-safe fluent interface. Adding a keyword is - accomplished by calling the same-name (usually) extension method. - docs: 'schema/basics/#schema-deserialization' - title: Deserializing a schema + By default, JSON Schema doesn't validate the `format` keyword. Instead, tools + are generally encouraged to provide a configuration option to enforce `format` validation. + + In _JsonSchema.Net_ that option is called `RequiredFormatValidation`. + docs: 'schema/basics/#schema-format' + title: 'Options: Validating Formats' instructions: | - Deserialize the text in `schemaText` into a `JsonSchema` variable called `schema`. + Configure the evaluation options to enable `format` validation. inputTemplate: '' contextCode: |- using System; @@ -172,36 +290,49 @@ public class Lesson : ILessonRunner { - public EvaluationResults Run(JsonObject context) + public EvaluationResults Run(JsonObject test) { - var instance = context["instance"]; - var schemaText = - """ - { - "type": "object", - "properties": { - "foo": { "type": "number", "minimum": 0 }, - "bar": { "type": "string" } - }, - "required": ["foo", "bar"] - } - """; + var instance = test["instance"]; + var format = test["format"].GetValue(); + JsonSchema schema = new JsonSchemaBuilder() + .Type(SchemaValueType.String) + .Format(format); + + var options = new EvaluationOptions(); /* USER CODE */ - return schema.Evaluate(instance); + return schema.Evaluate(instance, options); } } tests: - - instance: { "foo": 13, "bar": "a string" } + - instance: 2015-06-13T10:31:16+04:00 + format: date-time isValid: true - - instance: { "foo": false, "bar": "a string" } + - instance: 2015-06-13 + format: date-time isValid: false - - instance: { "foo": 13 } + - instance: 1.1.1.1 + format: ipv4 + isValid: true + - instance: www.google.com + format: ipv4 isValid: false - - instance: { "bar": "a string" } + - instance: '/json/pointer' + format: json-pointer + isValid: true + - instance: '/inva~lid/pointer' + format: json-pointer isValid: false - - instance: [1,2,3] + # - instance: '0/json/pointer' + # format: relative-json-pointer + # isValid: true + - instance: {} + format: date-time + isValid: false + - instance: [] + format: date-time isValid: false - instance: 6.8 - isValid: false \ No newline at end of file + format: date-time + isValid: false diff --git a/index.html b/index.html index 0659bbf..b6aa978 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@ - + Learn json-everything.net diff --git a/json-schema/index.html b/json-schema/index.html index 0659bbf..b6aa978 100644 --- a/json-schema/index.html +++ b/json-schema/index.html @@ -4,7 +4,7 @@ - + Learn json-everything.net