Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend user-defined keywords' context #68

Closed
alexberdnik opened this issue Jun 29, 2022 · 9 comments
Closed

Extend user-defined keywords' context #68

alexberdnik opened this issue Jun 29, 2022 · 9 comments

Comments

@alexberdnik
Copy link

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.

@santhosh-tekuri
Copy link
Owner

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.

@santhosh-tekuri
Copy link
Owner

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

@alexberdnik
Copy link
Author

alexberdnik commented Jul 6, 2022

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.

@santhosh-tekuri
Copy link
Owner

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.

@alexberdnik
Copy link
Author

alexberdnik commented Jul 9, 2022

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.

@santhosh-tekuri
Copy link
Owner

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.

@alexberdnik
Copy link
Author

alexberdnik commented Jul 9, 2022

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
}

@santhosh-tekuri
Copy link
Owner

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.

@alexberdnik
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants