Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
github.com/santhosh-tekuri/jsonschema/v5
Package jsonschema provides json-schema compilation and validation.
Compiler.AssertFormat
, Compiler.AssertContent
to true
see examples in godoc
The schema is compiled against the version specified in $schema
property.
If "$schema" property is missing, it uses latest draft which currently implemented
by this library.
You can force to use specific version, when $schema
is missing, as follows:
compiler := jsonschema.NewCompiler()
compiler.Draft = jsonschema.Draft4
This package supports loading json-schema from filePath and fileURL.
To load json-schema from HTTPURL, add following import:
import _ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
The ValidationError returned by Validate method contains detailed context to understand why and where the error is.
schema.json:
{
"$ref": "t.json#/definitions/employee"
}
t.json:
{
"definitions": {
"employee": {
"type": "string"
}
}
}
doc.json:
1
assuming err
is the ValidationError returned when doc.json
validated with schema.json
,
fmt.Printf("%#v\n", err) // using %#v prints errors hierarchy
Prints:
[I#] [S#] doesn't validate with file:///Users/santhosh/jsonschema/schema.json#
[I#] [S#/$ref] doesn't validate with 'file:///Users/santhosh/jsonschema/t.json#/definitions/employee'
[I#] [S#/definitions/employee/type] expected string, but got number
Here I
stands for instance document and S
stands for schema document.
The json-fragments that caused error in instance and schema documents are represented using json-pointer notation.
Nested causes are printed with indent.
To output err
in flag
output format:
b, _ := json.MarshalIndent(err.FlagOutput(), "", " ")
fmt.Println(string(b))
Prints:
{
"valid": false
}
To output err
in basic
output format:
b, _ := json.MarshalIndent(err.BasicOutput(), "", " ")
fmt.Println(string(b))
Prints:
{
"valid": false,
"errors": [
{
"keywordLocation": "",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/schema.json#",
"instanceLocation": "",
"error": "doesn't validate with file:///Users/santhosh/jsonschema/schema.json#"
},
{
"keywordLocation": "/$ref",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/schema.json#/$ref",
"instanceLocation": "",
"error": "doesn't validate with 'file:///Users/santhosh/jsonschema/t.json#/definitions/employee'"
},
{
"keywordLocation": "/$ref/type",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/t.json#/definitions/employee/type",
"instanceLocation": "",
"error": "expected string, but got number"
}
]
}
To output err
in detailed
output format:
b, _ := json.MarshalIndent(err.DetailedOutput(), "", " ")
fmt.Println(string(b))
Prints:
{
"valid": false,
"keywordLocation": "",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/schema.json#",
"instanceLocation": "",
"errors": [
{
"valid": false,
"keywordLocation": "/$ref",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/schema.json#/$ref",
"instanceLocation": "",
"errors": [
{
"valid": false,
"keywordLocation": "/$ref/type",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/t.json#/definitions/employee/type",
"instanceLocation": "",
"error": "expected string, but got number"
}
]
}
]
}
to install go install github.com/santhosh-tekuri/jsonschema/cmd/jv@latest
jv [-draft INT] [-output FORMAT] [-assertformat] [-assertcontent] <json-schema> [<json-or-yaml-doc>]...
-assertcontent
enable content assertions with draft >= 2019
-assertformat
enable format assertions with draft >= 2019
-draft int
draft used when '$schema' attribute is missing. valid values 4, 5, 7, 2019, 2020 (default 2020)
-output string
output format. valid values flag, basic, detailed
if no <json-or-yaml-doc>
arguments are passed, it simply validates the <json-schema>
.
if $schema
attribute is missing in schema, it uses latest version. this can be overridden by passing -draft
flag
exit-code is 1, if there are any validation errors
jv
can also validate yaml files. It also accepts schema from yaml files.
since yaml supports non-string keys, such yaml documents are rendered as invalid json documents.
most yaml parser use map[interface{}]interface{}
for object,
whereas json parser uses map[string]interface{}
.
so we need to manually convert them to map[string]interface{}
.
below code shows such conversion by toStringKeys
function.
https://play.golang.org/p/Hhax3MrtD8r
NOTE: if you are using gopkg.in/yaml.v3
, then you do not need such conversion. since this library
returns map[string]interface{}
if all keys are strings.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.