santhosh-tekuri/jsonschema

Extend user-defined keywords' context

Closed this issue · 9 comments

I'm trying to define a keyword to validate uri-reference (e.g. for "data/client.json" make sure that client.json file exists and it is a valid json schema.
Current context for user-defined keyword contains only current data, without information about the current document. So it is not possible to validate the desired logic.
Do you think it is possible to extend the context with document location uri/document itself?
I can image that this can be useful for other validation types too.

see the example

line 25 shows how it extracted the value of the custom keyword powerOf.
same way you can extract the value of your keyword which gives the uri-reference in string format.

got it. you need the location the the json document that is being verified.

the library don't ask for the document location. the user parses the json himself and calls Schema.Validate(interface{})

Well, here an example:

{
  "$id": "https://some.doc/doc1",
  "links": [
    "link": {
      "ref": "some/data-model.json"
    }
  ]
}

I want to validate that the "ref" is referencing to an existing document. For that I need to construct full URL: "https://some.doc/doc1/some/data-model.json". It is not possible to do that, because the context doesn't provide any information about current document.

assuming the example you have provided is instance document, you want to validate some/data-model.json relative to instance document. this library no where captures the instance document location. you have to do this at your end.

Again, I don't really need an instance location. I need an access to currently validated document (in addition to currently validated value) so I can extract the value from "$id" property. For example above, simplified schema could be:

"$id": {
  "type": "string",
  "format": "uri"
}
"links": {
  "type": "array",
  "items": {
    "type": "string",
    "format": "uri-schema",
    "validJson": true
  }
}

Where "validJson" is my custom keyword.

you are actually trying to validate the document that is being referred to using validJson Keyword.

I do not think i can pass the while instance document for validation(currently only the portion that is documented is passed)

I can suggest a workaround. since each instance document has single $id you can extract its value before validation and use it in your extension.

I currently use the workaround you mentioned, however I thought that it could be useful to have the whole document during the validation. This will add more possibilities for custom keywords.

type ValidationContext struct {
        document interface {}
	result          validationResult
	validate        func(sch *Schema, schPath string, v interface{}, vpath string) error
	validateInplace func(sch *Schema, schPath string) error
	validationError func(keywordPath string, format string, a ...interface{}) *ValidationError
}

problem is: func (s *Schema) validate(scope []schemaRef, vscope int, spath string, v interface{}, vloc string) is called recursively and when creating ValidationContext I do not have reference to the whole document.

otherwise, I have to modify func (s *Schema) validate(...) to add another argument to pass the while document along the call chain.

yep, it looks it can't be done without modification of validate function