![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
github.com/lestrrat-go/jsval
Validator toolset, with code generation from JSON Schema
The go-jsval
package is a data validation toolset, with
a tool to generate validators in Go from JSON schemas.
Read a schema file and create a validator using jsval
command:
jsval -s /path/to/schema.json -o validator.go
jsval -s /path/to/hyperschema.json -o validator.go -p "/links/0/schema" -p "/links/1/schema" -p "/links/2/schema"
Read a schema file and create a validator programatically:
package jsval_test
import (
"log"
"github.com/lestrrat-go/jsschema"
"github.com/lestrrat-go/jsval/builder"
)
func ExampleBuild() {
s, err := schema.ReadFile(`/path/to/schema.json`)
if err != nil {
log.Printf("failed to open schema: %s", err)
return
}
b := builder.New()
v, err := b.Build(s)
if err != nil {
log.Printf("failed to build validator: %s", err)
return
}
var input interface{}
if err := v.Validate(input); err != nil {
log.Printf("validation failed: %s", err)
return
}
}
Build a validator by hand:
func ExampleManual() {
v := jsval.Object().
AddProp(`zip`, jsval.String().RegexpString(`^\d{5}$`)).
AddProp(`address`, jsval.String()).
AddProp(`name`, jsval.String()).
AddProp(`phone_number`, jsval.String().RegexpString(`^[\d-]+$`)).
Required(`zip`, `address`, `name`)
var input interface{}
if err := v.Validate(input); err != nil {
log.Printf("validation failed: %s", err)
return
}
}
go get -u github.com/lestrrat-go/jsval
If you want to install the jsval
tool, do
go get -u github.com/lestrrat-go/jsval/cmd/jsval
The following command creates a file named jsval.go
which contains various variables containing *jsval.JSVal
structures so you can include them in your code:
jsval -s schema.json -o jsval.go
See the file generated_validator_test.go
for a sample
generated from JSON Schema schema.
If your document isn't a real JSON schema but contains one
or more JSON schema (like JSON Hyper Schema) somewhere inside
the document, you can use the -p
argument to access a
specific portion of a JSON document:
jsval -s hyper.json -p "/links/0" -p "/lnks/1"
This will generate a set of validators, with JSON references
within the file hyper.json
properly resolved.
Note: Not very well tested. Test cases welcome
This packages tries to handle JSON References properly.
For example, in the schema below, "age" input is validated
against the positiveInteger
schema:
{
"definitions": {
"positiveInteger": {
"type": "integer",
"minimum": 0,
}
},
"properties": {
"age": { "$ref": "#/definitions/positiveInteger" }
}
}
jsval server -listen :8080
You can specify a JSON schema, and see what kind of validator gets generated.
With maps, it's easy to check if a property exists. But if you are validating a struct, however, all of the fields exist all the time, and you basically cannot detect if you have a missing field to apply defaults, etc.
For such cases you should use the Maybe
interface provided in this package:
type Foo struct {
Name MaybeString `json:"name"`
}
This will declare the value as "optional", and the JSVal validation mechanism does the correct thing to process this field.
Name | Notes |
---|---|
go-jsschema | JSON Schema implementation |
go-jshschema | JSON Hyper Schema implementation |
go-jsref | JSON Reference implementation |
go-jspointer | JSON Pointer implementations |
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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.