Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example cannot excute. #77

Closed
hscspring opened this issue Aug 24, 2020 · 2 comments · Fixed by #78
Closed

Example cannot excute. #77

hscspring opened this issue Aug 24, 2020 · 2 comments · Fixed by #78
Assignees
Labels

Comments

@hscspring
Copy link

When I run go install, I get

❯ go install
# gts
./gts.go:7:3: imported and not used: "github.com/qri-io/jsonschema"
./gts.go:36:10: undefined: Schema

Is there anything wrong?

@Arqu
Copy link
Contributor

Arqu commented Aug 25, 2020

Thank you for reporting this, the README is a bit out of date. Bellow is the "fixed" example and the docs will be updated soon.

package main

import (
	"encoding/json"
	"fmt"
  "context"

	"github.com/qri-io/jsonschema"
)

func main() {
  ctx := context.Background()
	var schemaData = []byte(`{
    "$id": "https://qri.io/schema/",
    "$comment" : "sample comment",
    "title": "Person",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "age": {
            "description": "Age in years",
            "type": "integer",
            "minimum": 0
        },
        "friends": {
          "type" : "array",
          "items" : { "title" : "REFERENCE", "$ref" : "#" }
        }
    },
    "required": ["firstName", "lastName"]
  }`)

  rs := &jsonschema.Schema{}
  if err := json.Unmarshal(schemaData, rs); err != nil {
    panic("unmarshal schema: " + err.Error())
  }

  var valid = []byte(`{
    "firstName" : "George",
    "lastName" : "Michael"
    }`)
  errs, err := rs.ValidateBytes(ctx, valid)
  if err != nil {
    panic(err)
  }

  if len(errs) > 0 {
    fmt.Println(errs[0].Error())
  }

  var invalidPerson = []byte(`{
    "firstName" : "Prince"
    }`)

  errs, err = rs.ValidateBytes(ctx, invalidPerson)
  if err != nil {
    panic(err)
  }
  if len(errs) > 0 {
    fmt.Println(errs[0].Error())
  }

  var invalidFriend = []byte(`{
    "firstName" : "Jay",
    "lastName" : "Z",
    "friends" : [{
      "firstName" : "Nas"
      }]
    }`)
  errs, err = rs.ValidateBytes(ctx, invalidFriend)
  if err != nil {
    panic(err)
  }
  if len(errs) > 0 {
    fmt.Println(errs[0].Error())
  }
}

Feel free to re-open if things don't sit well after this.

@Arqu Arqu closed this as completed Aug 25, 2020
@feep feep added the docs label Aug 25, 2020
@feep feep self-assigned this Aug 25, 2020
@hscspring
Copy link
Author

Oh, thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants