Giter Club home page Giter Club logo

Comments (10)

santhosh-tekuri avatar santhosh-tekuri commented on May 24, 2024 3

@schneefisch

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.

santhosh-tekuri avatar santhosh-tekuri commented on May 24, 2024 1

@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.

santhosh-tekuri avatar santhosh-tekuri commented on May 24, 2024 1

637fbb2 with this commit you no longer need to register Compiler.Foramts["regex"]

from jsonschema.

schneefisch avatar schneefisch commented on May 24, 2024

Related to #61
and bbc9f89

from jsonschema.

schneefisch avatar schneefisch commented on May 24, 2024

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.

santhosh-tekuri avatar santhosh-tekuri commented on May 24, 2024

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.

santhosh-tekuri avatar santhosh-tekuri commented on May 24, 2024

@schneefisch

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.

schneefisch avatar schneefisch commented on May 24, 2024

Wow, that was quick
I will check that out immediately ;)

from jsonschema.

schneefisch avatar schneefisch commented on May 24, 2024

Indeed, My use-case seems fixed.
Thank you so much for that lightning-speed response and fix. That was amazing.

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on May 24, 2024

d265bc9 adds example test case

from jsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.