Comments (10)
now meta schemas are compiled with custom regex engine if needed. see 333d938
this should fix the issue completely. could you please re-verify at your end.
from jsonschema.
@schneefisch
as you pointed out I am still using native-regex. I can fix those things that you mentioned.
but there is another issue. the draft metaschema are precompiled in package init
this pre-compilation will end up using go-native regex.
in current implementation we precompile draft meta schemas once during package initialisation and use it during user schema compilation
so this feature becomes half backed.
one choice is compile draft metaschemas for each user schema compilation which seems redundant.
do you have any suggestions.
from jsonschema.
637fbb2 with this commit you no longer need to register Compiler.Foramts["regex"]
from jsonschema.
from jsonschema.
Thats an interesting suggestion, I did not see that so far.
so sadly I do not yet have a suggestion. Maybe I can find something.
from jsonschema.
I think we can recompile draft metaschemas only when user has overridden regex impl in compiler.
This should not affect the performance for those who are using native-regex.
let me work on this
from jsonschema.
I have fixed main issues you have mentioned with d33a6df
I have tested it with following program:
package main
import (
"encoding/json"
"strings"
"github.com/dlclark/regexp2"
"github.com/santhosh-tekuri/jsonschema/v5"
)
var schema = `{
"patternProperties": {
"^a.*" : { "type": "number" }
}
}`
var instance = `{ "a": "34"}`
func main() {
compiler := jsonschema.NewCompiler()
compiler.CompileRegex = func(s string) (jsonschema.Regexp, error) {
println("compiling regex: ", s)
re, err := regexp2.Compile(s, regexp2.ECMAScript)
if err != nil {
return nil, err
}
return ecmaRegex{re}, nil
}
compiler.Formats["regex"] = func(v interface{}) bool {
println("checking regex format")
s, ok := v.(string)
if !ok {
return true
}
_, err := regexp2.Compile(s, regexp2.ECMAScript)
return err == nil
}
if err := compiler.AddResource("schema.json", strings.NewReader(schema)); err != nil {
panic(err)
}
sch, err := compiler.Compile("schema.json")
if err != nil {
panic(err)
}
var inst interface{}
if err := json.Unmarshal([]byte(instance), &inst); err != nil {
panic(err)
}
if err := sch.Validate(inst); err != nil {
panic(err)
}
}
type ecmaRegex struct {
re *regexp2.Regexp
}
func (re ecmaRegex) MatchString(s string) bool {
println("matching regex: ", s)
matched, err := re.re.MatchString(s)
return err == nil && matched
}
func (re ecmaRegex) String() string {
return re.re.String()
}
when run it does prints from custom regex implementation as below:
$ go run .
compiling regex: ^a.*
matching regex: a
still the draft meta-schemas issue is not yet fixed. so there is still following limitation:
consider a schema as below:
{
"patternProperties": {
"XXX": {}
}
}
if XXX
is valid regex when using github.com/dlclark/regexp2
but not with native-regex, you would expect the above schema to compile. but it fails to compile because the schema itself is validated with draft meta-schema(which is currently compiled with native-regex)
from jsonschema.
Wow, that was quick
I will check that out immediately ;)
from jsonschema.
Indeed, My use-case seems fixed.
Thank you so much for that lightning-speed response and fix. That was amazing.
from jsonschema.
d265bc9 adds example test case
from jsonschema.
Related Issues (20)
- Provide CLI binaries with release HOT 3
- Link in README for YAML playground example is broken HOT 1
- `Compiler.AddResource` for adding pre-compiled schemas HOT 6
- Feature: Adding Context to Validate Method HOT 2
- implementing json.Marshaler interface HOT 9
- Create JSONSchema from Compile()'s result HOT 2
- Wrong unevaluatedProperties validation errors HOT 6
- compiler.go bug in 171
- Invalid JSON schema does not error out HOT 2
- [bug] Not reading from stdin HOT 2
- Missing document file path in validation output when validating multiple documents HOT 5
- Error messages are revealing absolute file paths HOT 1
- YouValidateMe JSON Schema library server project built on this library
- ECMA-262 as the default regex in this library? HOT 1
- jsonschema.Compiler has no field or method CompileRegex HOT 7
- Question: parsing Schema to string HOT 1
- error is missing instanceLocation HOT 1
- Cannot install older version than `@latest` HOT 3
- The latest release doesn't build HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonschema.