Giter Club home page Giter Club logo

Comments (5)

vishwanathj avatar vishwanathj commented on May 15, 2024

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

}

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on May 15, 2024

the test you provided is passing for me.

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

from jsonschema.

vishwanathj avatar vishwanathj commented on May 15, 2024

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

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on May 15, 2024

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}]}}}}}`

from jsonschema.

vishwanathj avatar vishwanathj commented on May 15, 2024

@santhosh-tekuri Thank you so much for your help

from jsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.