Comments (2)
ideally schema validation must be pure. I mean, if you repeatedly validate a json with schema, then it should always produce same result. if you have keyword that is making decisions based on the data from a database this will break.
ideally keywords are not supposed to do any IO during validation. So I am not keen in adding context.Context
to Validate
Method
but as a workaround for your use case:
you would want to pass different context.Context
for each validation. but the code you shown above is capturing ctx
during compilation. If you are always compiling before each validation (i.e discarding compiling schema after each validation) it might work
you can use https://github.com/timandy/routine
to pass context. but it uses internal goroutine ID for this (which may change in future Golang versions). you can use it as follows:
import "github.com/timandy/routine"
// create global variable for holding thread local context
var threadLocal = routine.NewThreadLocal[*context.Context]()
func doSchemaValidation(ctx *context.Context, sch *Schema, instance interface{}) error {
threadLocal.set(ctx)
}
type compiledMgsFormat struct {}
func (f compiledMgsFormat) Validate(ctx jsonschema.ValidationContext, v interface{}) error {
ctx := threadLocal.get()
//call to DB
}
from jsonschema.
I didn't specify use case: i make call to database in Validate
.
I've done bad WA which works only if jsonschema.NewCompiler()
is not singelton. Maybe you tell me the better solution. I guess the compiler must be singleton in production.
I simply passed the ctx through my own ExtCompiler and ExtSchema, like this:"
ctx:= context.Context
compiler := jsonschema.NewCompiler()
compiler.RegisterExtension("mgsFormat", MgsFormatMeta, mgsFormatCompiler{Ctx})
mgsFormatCompiler:
type mgsFormatCompiler struct {
ctx context.Context
}
func (compiler mgsFormatCompiler) Compile(ctx jsonschema.CompilerContext, m map[string]interface{}) (jsonschema.ExtSchema, error) {
if mgsFormatRaw, ok := m["mgsFormat"]; ok {
return compiledMgsFormat{compiler.ctx}, nil
}
return nil, nil
}
compiledMgsFormat
type compiledMgsFormat struct {
ctx context.Context
}
func (f compiledMgsFormat) Validate(ctx jsonschema.ValidationContext, v interface{}) error {
ctx := f.ctx
//call to DB
}
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
- 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.