jsonschema icon indicating copy to clipboard operation
jsonschema copied to clipboard

Validator incorrectly accepts strings for booleans

Open polarina opened this issue 4 years ago • 0 comments

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())
	}
}

polarina avatar Nov 09 '20 09:11 polarina