Skip to content

Commit

Permalink
ISBN regex in VOR schema is now patched because it can't be compiled …
Browse files Browse the repository at this point in the history
…in Go.
  • Loading branch information
lsh-0 committed Oct 9, 2023
1 parent e3b2b45 commit da06a19
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
require (
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.15.0 h1:5n/pM+v3r5ujuNl4YLZLsQ+UE5jlkLVm7jMzT5Mpolw=
github.com/tidwall/gjson v1.15.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
Expand Down
35 changes: 20 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main
// - https://dev.to/vearutop/benchmarking-correctness-and-performance-of-go-json-schema-validators-3247

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand All @@ -20,12 +21,9 @@ import (

"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

// todo:
// https://json-schema.org/understanding-json-schema/reference/regular_expressions.html
// https://github.com/santhosh-tekuri/jsonschema/issues/113

func panic_on_err(err error, action string) {
if err != nil {
panic(fmt.Sprintf("failed with '%s' while '%s'", err.Error(), action))
Expand All @@ -39,20 +37,31 @@ type Schema struct {
}

func configure_validator(schema_root string) map[string]Schema {
loader := jsonschema.Loaders["file"]
c := jsonschema.NewCompiler()
c.Draft = jsonschema.Draft4
compiler := jsonschema.NewCompiler()
compiler.Draft = jsonschema.Draft4
schema_file_list := map[string]string{
"POA": path.Join(schema_root, "/dist/model/article-poa.v3.json"),
"VOR": path.Join(schema_root, "/dist/model/article-vor.v7.json"),
}

schema_map := map[string]Schema{}
for label, path := range schema_file_list {
rdr, err := loader(path)
panic_on_err(err, fmt.Sprintf("loading '%s' schema file: %s", label, path))
err = c.AddResource(label, rdr)
file_bytes, err := os.ReadFile(path)
panic_on_err(err, fmt.Sprintf("reading '%s' schema file: %s", label, path))
if label == "VOR" {
// patch ISBN regex as it can't be compiled in Go.
// todo: this needs a fix upstream.
// https://json-schema.org/understanding-json-schema/reference/regular_expressions.html
// https://github.com/santhosh-tekuri/jsonschema/issues/113
find := "allOf.2.properties.references.items.definitions.book.properties.isbn.pattern"
replace := "^.+$"
file_bytes, err = sjson.SetBytes(file_bytes, find, replace)
panic_on_err(err, fmt.Sprintf("patching ISBN in '%s' schema: %s", label, path))
}

err = compiler.AddResource(label, bytes.NewReader(file_bytes))
panic_on_err(err, "adding schema to compiler: "+label)
schema, err := c.Compile(label)
schema, err := compiler.Compile(label)
panic_on_err(err, "compiling schema: "+label)
schema_map[label] = Schema{
Label: label,
Expand All @@ -63,10 +72,6 @@ func configure_validator(schema_root string) map[string]Schema {
return schema_map
}

func init() {

}

// ---

func read_article_data(article_json_path string) (string, interface{}) {
Expand Down

0 comments on commit da06a19

Please sign in to comment.