santhosh-tekuri/jsonschema

Working example of in-memory JSON schema and in-memory JSON data to be validated against the schema

Closed this issue · 5 comments

Lets say that I have in-memory schema defined as below:

var testJSONParamNonParamSchema = []byte(`
{
  "vmDeviceDefine": {
    "vm": {
      "additionalProperties": false, 
      "type": "object", 
      "required": [
        "vcpus"
      ], 
      "optional": [
        "memory"
      ], 
      "properties": {
        "vcpus": {
          "oneOf": [
            {
              "pattern": "^\\$[A-Za-z][-A-Za-z0-9_]*$", 
              "type": "string"
            }, 
            {
              "minimum": 2, 
              "type": "integer", 
              "maximum": 16, 
              "multipleOf": 2.0
            }
          ]
        },
        "memory": {
          "oneOf": [
            {
              "pattern": "^\\$[A-Za-z][-A-Za-z0-9_]*$", 
              "type": "string"
            }, 
            {
              "minimum": 512, 
              "type": "integer", 
              "maximum": 16384, 
              "multipleOf": 512
            }
          ]
        }
      }
    }
  }
}
`)

and JSON data (in-memory and not in a file) to be validated as below

var dataToBeValidatedAgainstSchema := `{"vm": {"vcpus": "$vcpus","memory": "$memory"}}`

How could I use this library to achieve the above? Is it possible? would be great if I could be pointed to an example on how to achieve the above?
THanks

I tried to write a TestFunction for positive and negative testing but does not seem to work as expected, looks like I am doing something wrong, appreciate any help

func TestValidateJSONBufAgainstSchema2(t *testing.T) {
	data := `{"vmDeviceDefine": {"vm": {"additionalProperties": false, "type": "object", "required": ["vcpus"], "optional": ["memory"], "properties": {"vcpus": {"oneOf": [{"pattern": "^\\$[A-Za-z][-A-Za-z0-9_]*$", "type": "string"}, {"minimum": 2, "type": "integer", "maximum": 16, "multipleOf": 2.0}]},"memory": {"oneOf": [{"pattern": "^\\$[A-Za-z][-A-Za-z0-9_]*$", "type": "string"}, {"minimum": 512, "type": "integer", "maximum": 16384, "multipleOf": 512}]}}}}}`
	url := "sch.json"
	compiler := jsonschema.NewCompiler()
	if err := compiler.AddResource(url, strings.NewReader(string(data))); err != nil {
		t.Log(err)
		t.Error(err)
	}
	schema, err := compiler.Compile(url)
	if err != nil {
		t.Log(err)
		t.Error(err)
	}

	//Expect this to PASS
	if err = schema.Validate(strings.NewReader(`{"vm": {"vcpus": "$vcpus","memory": "$memory"}}`)); err != nil {
		t.Log(err)
		t.Error(err)
	}

	// Expecting this to thrown an error as the required "vcpus" defined in schema is missing
	if err = schema.Validate(strings.NewReader(`{"vm": {"pus": "$vcpus","memory": "$memory"}}`)); err != nil {
		t.Log(err)
		t.Error(err)
	}

}

the test you provided is passing for me.

check which version of jsonschema you are using. and what is the test failure error message?

@santhosh-tekuri I am expecting to see a failure for the below "if" statement, but is passing. I am expecting to see an error mentioning that the required attrubute "vcpus" is missing. "vcpus" is a required attribute defined in the schema. I am using the jsonschema version from Dec 24th 2018

// Expecting this to thrown an error as the required "vcpus" defined in schema is missing
	if err = schema.Validate(strings.NewReader(`{"vm": {"pus": "$vcpus","memory": "$memory"}}`)); err != nil {
		t.Log(err)
		t.Error(err)
	}

your jsonschema is incorrect. below is the correct one:

data := `{"type": "object", "properties": {"vm": {"additionalProperties": false, "type": "object", "required": ["vcpus"], "optional": ["memory"], "properties": {"vcpus": {"oneOf": [{"pattern": "^\\$[A-Za-z][-A-Za-z0-9_]*$", "type": "string"}, {"minimum": 2, "type": "integer", "maximum": 16, "multipleOf": 2.0}]},"memory": {"oneOf": [{"pattern": "^\\$[A-Za-z][-A-Za-z0-9_]*$", "type": "string"}, {"minimum": 512, "type": "integer", "maximum": 16384, "multipleOf": 512}]}}}}}`

@santhosh-tekuri Thank you so much for your help