Comments (8)
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.
if you want to locate with your own logic, use jsonschema.Loaders
see example
from jsonschema.
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.
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.
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.
this example demonstrates how to load schemas from map
from jsonschema.
The readme mentions schema introspection, does the package provide a way to generate types/structs/interfaces from the schemas?
from jsonschema.
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)
- Draft 2020-12 and array's "items" HOT 2
- Can add `Loader` , give schema and `[]byte` , parse to `map` or `struct` ? HOT 1
- pre-commit hook HOT 3
- request: validate examples HOT 1
- Can not install v5.2.0 HOT 7
- False positive error for a valid regex pattern HOT 1
- invalid jsonType: time.Time HOT 3
- Compiler short circuits when validating schemas, not providing the full list of errors
- Extension.Validate provides data in inconsistent order HOT 1
- Getting property name when compiling and validating user-defined keyword via extensions HOT 5
- Feature request: Pass custom compiler into "convenience" functions like CompileString HOT 2
- Performance drop when schema has "uniqueItems" set to true HOT 7
- `format` field of json schemas is not being validated HOT 1
- CompileString - stat .: permission denied HOT 1
- How can I validate to Python re flavor regex? HOT 4
- Proposal: Add Method to Generate Go Types from JSON Schema HOT 1
- Error when loading valid draft7 json schema HOT 2
- Validating json with json-schema HOT 4
- Draft2020 accepts invalid email address. HOT 5
- Support for $defs and definition keyword in Schema object. HOT 3
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.