mirror of
https://github.com/Smaug123/gitea-repo-config
synced 2025-10-11 10:18:40 +00:00
Express discriminated union in JSON schema (#7)
This commit is contained in:
@@ -3,53 +3,39 @@ namespace Gitea.Declarative.Test
|
||||
open System.IO
|
||||
open System.Reflection
|
||||
open Gitea.Declarative
|
||||
open NJsonSchema.Generation
|
||||
open NJsonSchema.Validation
|
||||
open NUnit.Framework
|
||||
open FsUnitTyped
|
||||
open NJsonSchema
|
||||
open Newtonsoft.Json
|
||||
open Newtonsoft.Json.Schema
|
||||
open Newtonsoft.Json.Schema.Generation
|
||||
open Newtonsoft.Json.Serialization
|
||||
|
||||
[<TestFixture>]
|
||||
module TestSchema =
|
||||
let schemaGen = JSchemaGenerator ()
|
||||
schemaGen.ContractResolver <- CamelCasePropertyNamesContractResolver ()
|
||||
|
||||
[<Test>]
|
||||
let ``Schema is consistent`` () =
|
||||
let schemaFile =
|
||||
Assembly.GetExecutingAssembly().Location
|
||||
|> FileInfo
|
||||
|> fun fi -> fi.Directory
|
||||
|> Utils.findFileAbove "Gitea.Declarative.Lib/GiteaConfig.schema.json"
|
||||
|
||||
let existing = JSchema.Parse (File.ReadAllText schemaFile.FullName)
|
||||
let derived = schemaGen.Generate typeof<SerialisedGiteaConfig>
|
||||
|
||||
existing.ToString () |> shouldEqual (derived.ToString ())
|
||||
|
||||
[<Test>]
|
||||
let ``Example conforms to schema`` () =
|
||||
let executing = Assembly.GetExecutingAssembly().Location |> FileInfo
|
||||
let schemaFile = Utils.findFileAbove "GiteaConfig.json" executing.Directory
|
||||
|
||||
let existing = JSchema.Parse (File.ReadAllText schemaFile.FullName)
|
||||
let schemaFile =
|
||||
Utils.findFileAbove "Gitea.Declarative.Lib/GiteaConfig.schema.json" executing.Directory
|
||||
|
||||
let schema = JsonSchema.FromJsonAsync(File.ReadAllText schemaFile.FullName).Result
|
||||
|
||||
let jsonFile = Utils.findFileAbove "GiteaConfig.json" executing.Directory
|
||||
let json = File.ReadAllText jsonFile.FullName
|
||||
|
||||
use reader = new JsonTextReader (new StringReader (json))
|
||||
use validatingReader = new JSchemaValidatingReader (reader)
|
||||
validatingReader.Schema <- existing
|
||||
let validator = JsonSchemaValidator ()
|
||||
let errors = validator.Validate (json, schema)
|
||||
|
||||
let messages = ResizeArray ()
|
||||
validatingReader.ValidationEventHandler.Add (fun args -> messages.Add args.Message)
|
||||
errors |> shouldBeEmpty
|
||||
|
||||
let ser = JsonSerializer ()
|
||||
ser.ContractResolver <- CamelCasePropertyNamesContractResolver ()
|
||||
let _config = ser.Deserialize<SerialisedGiteaConfig> validatingReader
|
||||
|
||||
messages |> shouldBeEmpty
|
||||
[<Test>]
|
||||
let ``Example can be loaded`` () =
|
||||
let executing = Assembly.GetExecutingAssembly().Location |> FileInfo
|
||||
let jsonFile = Utils.findFileAbove "GiteaConfig.json" executing.Directory
|
||||
GiteaConfig.get jsonFile |> ignore
|
||||
|
||||
[<Test>]
|
||||
[<Explicit "Run this to regenerate the schema file">]
|
||||
@@ -60,6 +46,27 @@ module TestSchema =
|
||||
|> fun fi -> fi.Directory
|
||||
|> Utils.findFileAbove "Gitea.Declarative.Lib/GiteaConfig.schema.json"
|
||||
|
||||
let schema = schemaGen.Generate typeof<SerialisedGiteaConfig>
|
||||
let settings = JsonSchemaGeneratorSettings ()
|
||||
|
||||
File.WriteAllText (schemaFile.FullName, schema.ToString ())
|
||||
settings.SerializerSettings <-
|
||||
JsonSerializerSettings (ContractResolver = CamelCasePropertyNamesContractResolver ())
|
||||
|
||||
let schema = JsonSchema.FromType (typeof<SerialisedGiteaConfig>, settings)
|
||||
|
||||
// Hack around the lack of discriminated unions in C#
|
||||
let serialisedRepoSchema = schema.Definitions.[typeof<SerialisedRepo>.Name]
|
||||
serialisedRepoSchema.RequiredProperties.Clear ()
|
||||
|
||||
do
|
||||
let schema = JsonSchema ()
|
||||
schema.RequiredProperties.Add "description"
|
||||
schema.RequiredProperties.Add "gitHub"
|
||||
serialisedRepoSchema.OneOf.Add schema
|
||||
|
||||
do
|
||||
let schema = JsonSchema ()
|
||||
schema.RequiredProperties.Add "description"
|
||||
schema.RequiredProperties.Add "native"
|
||||
serialisedRepoSchema.OneOf.Add schema
|
||||
|
||||
File.WriteAllText (schemaFile.FullName, schema.ToJson ())
|
||||
|
Reference in New Issue
Block a user