qri-io / jsonschema

golang implementation of https://json-schema.org drafts 7 & 2019-09
MIT License
461 stars 54 forks source link

Validator incorrectly accepts strings for booleans #86

Open polarina opened 4 years ago

polarina commented 4 years ago

Hi,

The JSON schema validator incorrectly accepts the JSON strings "true" and "false" for booleans.

Example schema:

{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "type": "boolean"
}

Example input:

"true"

Expected error:

/: "true" type should be boolean, got string

Observed behaviour: Validation passes without errors, which was not expected.

Program:

package main

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

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

func main() {
    ctx := context.Background()
    var schemaData = []byte(`{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "type": "boolean"
  }`)

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

    var invalid = []byte(`"true"`)
    errs, err := rs.ValidateBytes(ctx, invalid)
    if err != nil {
        panic(err)
    }

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