GithubHelp home page GithubHelp logo

Comments (7)

santhosh-tekuri avatar santhosh-tekuri commented on July 17, 2024

Custom regexp support is not yet released.

there is v6.0.0-beta1 released from https://github.com/santhosh-tekuri/jsonschema/tree/boon
Note it is a major version change, so some api's you are using are changed slightly different.

this version has custom regexp support

from jsonschema.

crowdwave avatar crowdwave commented on July 17, 2024

I tried that version it didn't work.

Do you have example code that compiles showing it working?

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on July 17, 2024

see example https://github.com/santhosh-tekuri/jsonschema/blob/boon/example_regexp_test.go

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on July 17, 2024

you have to use go.mod in order to use 6.0.0-beta1

you can try with following go.mod file:

module example.com/ex

go 1.21.1

require (
        github.com/dlclark/regexp2 v1.11.0
        github.com/santhosh-tekuri/jsonschema/v6 v6.0.0-beta1
)

require golang.org/x/text v0.14.0 // indirect

from jsonschema.

crowdwave avatar crowdwave commented on July 17, 2024

Below is the code from: https://github.com/santhosh-tekuri/jsonschema/blob/boon/example_regexp_test.go

It compiles.

But when I add in this line:
c.Draft = jsonschema.Draft7

It no longer compiles, and gives this error:

go build -o test test.go

./test.go:49:4: c.Draft undefined (type *jsonschema.Compiler has no field or method Draft)

package jsonschema_test

import (
	"fmt"
	"log"
	"strings"

	"github.com/dlclark/regexp2"
	"github.com/santhosh-tekuri/jsonschema/v6"
)

type dlclarkRegexp regexp2.Regexp

func (re *dlclarkRegexp) MatchString(s string) bool {
	matched, err := (*regexp2.Regexp)(re).MatchString(s)
	return err == nil && matched
}

func (re *dlclarkRegexp) String() string {
	return (*regexp2.Regexp)(re).String()
}

func dlclarkCompile(s string) (jsonschema.Regexp, error) {
	re, err := regexp2.Compile(s, regexp2.ECMAScript)
	if err != nil {
		return nil, err
	}
	return (*dlclarkRegexp)(re), nil
}

// Example_customRegexpEngine shows how to use dlclark/regexp2
// instead of regexp from standard library.
func Example_customRegexpEngine() {
	// golang regexp does not support escape sequence: `\c`
	schema, err := jsonschema.UnmarshalJSON(strings.NewReader(`{
		"type": "string",
		"pattern": "^\\cc$"
	}`))
	if err != nil {
		log.Fatal(err)
	}
	inst, err := jsonschema.UnmarshalJSON(strings.NewReader(`"\u0003"`))
	if err != nil {
		log.Fatal(err)
	}

	c := jsonschema.NewCompiler()
	c.Draft = jsonschema.Draft7
	c.UseRegexpEngine(dlclarkCompile)
	if err := c.AddResource("schema.json", schema); err != nil {
		log.Fatal(err)
	}

	sch, err := c.Compile("schema.json")
	if err != nil {
		log.Fatal(err)
	}
	err = sch.Validate(inst)
	fmt.Println("valid:", err == nil)
	// Output:
	// valid: true
}

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on July 17, 2024

you have to do: c.DefaultDraft(jsonschema.Draft7)

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on July 17, 2024

use v6.0.0

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.