Giter Club home page Giter Club logo

Comments (8)

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

godoc contains examples showing the usage.

if your schema file uses other schema files, they are automatically located and compiled. for example consider following schema file /example/schema1.json with content:

{
    "properties": {
        "p1": {
            "type": "boolean"
         },
        "p2": {
            "$ref": "schema2.json"
          }
    }
}

and /example/schema2.json has following content:

{
    "type": "string"
}

then to compile the schema do the following:

	sch, err := jsonschema.Compile("/example/schema1.json")
	if err != nil {
		log.Fatalf("%#v", err)
	}

you can see that you are compiling /example/schema1.json. the compiler automatically located /example/schema2.json and compiles it.

to validate `/example/sample.json', do the following:

	data, err := ioutil.ReadFile("/example/sample.json")
	if err != nil {
		log.Fatal(err)
	}

	var v interface{}
	if err := json.Unmarshal(data, &v); err != nil {
		log.Fatal(err)
	}

	if err = sch.Validate(v); err != nil {
		log.Fatalf("%#v", err)
	}

from jsonschema.

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

if you want to locate with your own logic, use jsonschema.Loaders see example

from jsonschema.

GabenGar avatar GabenGar commented on May 14, 2024

Yeah but I am using draft7 so I have to go custom Compiler route. Do I understand it right I'll have to use Compiler.AddResource() to add json schemas and then Compiler.MustCompile() to create schemas out of them?

from jsonschema.

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

if the subschema is not in the expected location then Compiler.AddResource() has to be used.

say in the above example the compiler looks for schema2.json at location /example/schema2.json. for example say schema2.json is in directory /somedir/schema2.json file, then you have to do the following:

	schema2, err := ioutil.ReadFile("/somedir/schema2.json")
	if err != nil {
		log.Fatal(err)
	}
        compiler.AddResource("file:///example/schema2.json", schema2)

from jsonschema.

GabenGar avatar GabenGar commented on May 14, 2024

But reading from file system is sub-optimal and a security issue at runtime (i.e. the user machine can change schemas in-between program restarts).
Let's say I used codegen to create a map of schemas map[string][]byte, where key is $id of the schema and the value is its json literal converted into bytes.
How do I convert this map into the map/list of schemas usable by this package?

from jsonschema.

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

this example demonstrates how to load schemas from map

from jsonschema.

GabenGar avatar GabenGar commented on May 14, 2024

The readme mentions schema introspection, does the package provide a way to generate types/structs/interfaces from the schemas?

from jsonschema.

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

The Schema struct is exported in api. Gererating what you said can be implemented using the information in Schema struct

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.