Giter Club home page Giter Club logo

Comments (2)

voronovmaksim avatar voronovmaksim commented on May 28, 2024

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.

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

@voronovmaksim

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.

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.