Comments (7)
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.
I tried that version it didn't work.
Do you have example code that compiles showing it working?
from jsonschema.
see example https://github.com/santhosh-tekuri/jsonschema/blob/boon/example_regexp_test.go
from jsonschema.
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.
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.
you have to do: c.DefaultDraft(jsonschema.Draft7)
from jsonschema.
use v6.0.0
from jsonschema.
Related Issues (20)
- Provide CLI binaries with release HOT 3
- Link in README for YAML playground example is broken HOT 1
- Issue with custom Regex Library HOT 10
- `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
- 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.