Skip to content

Commit

Permalink
add json schema validation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Oct 28, 2022
1 parent 2366699 commit 325cb91
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/ISADotNet.Tests/ISADotNet.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="JsonSchemaValidation.fs" />
<Compile Include="Utils.fs" />
<Compile Include="ISADotnet\JsonExtensionsTests.fs" />
<Compile Include="ISADotnet\NameAndTypeCastingTests.fs" />
Expand All @@ -32,6 +33,9 @@

<ItemGroup>
<PackageReference Include="Expecto" Version="9.*" />
<PackageReference Include="FSharp.Data" Version="5.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.14" />
<PackageReference Include="YoloDev.Expecto.TestSdk" Version="0.*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.*" />
<PackageReference Update="FSharp.Core" Version="6.0.5" />
Expand Down
109 changes: 109 additions & 0 deletions tests/ISADotNet.Tests/JsonSchemaValidation.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
module JsonSchemaValidation

open Expecto
open FSharp.Data
open Newtonsoft.Json
open Newtonsoft.Json.Linq
open Newtonsoft.Json.Schema

module JSchema =

let validate (schemaBaseURL : string) (schemaURL : string) (objectString : string) : (bool * #seq<string>) =
let resolver = JSchemaUrlResolver()
let settings = JSchemaReaderSettings(Resolver = resolver,BaseUri = System.Uri(schemaBaseURL))

let schemaString = Http.RequestString schemaURL
let schemaReader = new JsonTextReader(new System.IO.StringReader(schemaString))

let schema = JSchema.Load(schemaReader,settings)
let objectJson = JObject.Parse(objectString)

objectJson.IsValid(schema)

module Expect =

let mutable schemaBaseURL = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/"

let matchingSchema (schemaURL : string) (objectString : string)=
let isValid,msg = JSchema.validate schemaBaseURL schemaURL objectString
Expect.isTrue isValid (sprintf "Json Object did not match Json Schema: %A" msg)

let matchingAssay (assayString : string) =
let assayUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/assay_schema.json"
matchingSchema assayUrl assayString

let matchingComment (commentString : string) =
let commentUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/comment_schema.json"
matchingSchema commentUrl commentString

let matchingData (dataString : string) =
let dataUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/data_schema.json"
matchingSchema dataUrl dataString

let matchingFactor (factorString : string) =
let factorUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/factor_schema.json"
matchingSchema factorUrl factorString

let matchingFactorValue (factorValueString : string) =
let factorValueUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/factor_value_schema.json"
matchingSchema factorValueUrl factorValueString

let matchingInvestigation (investigationString : string) =
let investigationUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/investigation_schema.json"
matchingSchema investigationUrl investigationString

let matchingMaterialAttribute (materialAttributeString : string) =
let materialAttributeUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/material_attribute_schema.json"
matchingSchema materialAttributeUrl materialAttributeString

let matchingMaterialAttributeValue (materialAttributeValueString : string) =
let materialAttributeValueUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/material_attribute_value_schema.json"
matchingSchema materialAttributeValueUrl materialAttributeValueString

let matchingMaterial (materialString : string) =
let materialUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/material_schema.json"
matchingSchema materialUrl materialString

let matchingOntologyAnnotation (ontologyAnnotationString : string) =
let ontologyAnnotationUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/ontology_annotation_schema.json"
matchingSchema ontologyAnnotationUrl ontologyAnnotationString

let matchingOntologySourceReference (ontologySourceReferenceString : string) =
let ontologySourceReferenceUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/ontology_source_reference_schema.json"
matchingSchema ontologySourceReferenceUrl ontologySourceReferenceString

let matchingPerson (personString : string) =
let personUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/person_schema.json"
matchingSchema personUrl personString

let matchingProcessParameterValue (processParameterValueString : string) =
let processParameterValueUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/process_parameter_value_schema.json"
matchingSchema processParameterValueUrl processParameterValueString

let matchingProcess (processString : string) =
let processUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/process_schema.json"
matchingSchema processUrl processString

let matchingProtocolParameter (protocolParameterString : string) =
let protocolParameterUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/protocol_parameter_schema.json"
matchingSchema protocolParameterUrl protocolParameterString

let matchingProtocol (protocolString : string) =
let protocolUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/protocol_schema.json"
matchingSchema protocolUrl protocolString

let matchingPublication (publicationString : string) =
let publicationUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/publication_schema.json"
matchingSchema publicationUrl publicationString

let matchingSample (sampleString : string) =
let sampleUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/sample_schema.json"
matchingSchema sampleUrl sampleString

let matchingSource (sourceString : string) =
let sourceUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/source_schema.json"
matchingSchema sourceUrl sourceString

let matchingStudy (studyString : string) =
let studyUrl = "https://raw.githubusercontent.com/ISA-tools/isa-specs/master/source/_static/isajson/study_schema.json"
matchingSchema studyUrl studyString

0 comments on commit 325cb91

Please sign in to comment.