Comments (15)
sample code for this:
package main
import (
"fmt"
"strings"
"github.com/santhosh-tekuri/jsonschema/v5"
)
var openAPIDocument = `
{
"components": {
"schemas": {
"TestObject": {
"properties": {
"test": {
"$ref": "#/components/schemas/my-string"
},
"name": {
"type": "string"
}
},
"type": "object"
},
"my-string": {
"pattern": "^test-*.",
"type": "string"
}
}
}
}
`
func main() {
compiler := jsonschema.NewCompiler()
if err := compiler.AddResource("openapidocument.json", strings.NewReader(openAPIDocument)); err != nil {
panic(err)
}
schema, err := compiler.Compile("openapidocument.json#/components/schemas/TestObject")
if err != nil {
panic(err)
}
_ = schema
fmt.Println("compilation successfull")
}
from jsonschema.
I am not familiar with openAPI.
it would be helpful, if you can illustrate your issue with sample openAPI document, and what you are trying to accomplish.
i do not understand why AddResource
and Compile
are not enough for your use-case.
from jsonschema.
I'm also looking into using this library in kin-openapi. The reason why AddResource isn't sufficient for this use case is that the root openapi document is usually referable by just #
, however, the AddResource clearly doesn't allow this behaviour:
func newResource(url string, r io.Reader) (*resource, error) {
if strings.IndexByte(url, '#') != -1 {
panic(fmt.Sprintf("BUG: newResource(%q)", url))
}
An example simplified openapi doc:
components:
schemas:
TestObject:
properties:
test:
$ref: "#/components/schemas/my-string"
name:
type: string
type: object
my-string:
pattern: "^test-*."
type: string
If this makes sense to you I wouldn't mind attempting a PR.
from jsonschema.
Actually I've figured out a way to achieve this with extensions. Very nifty stuff.
from jsonschema.
@lkm Do you mind sharing your solution or at least the big picture of it?
from jsonschema.
@fenollp of course I plan to submit a PR to kin-openapi with a proof of concept, just wanted to get it tidied up and pass a few more tests first.
from jsonschema.
@lkm you do not need to create extension for this. see here how to do this.
from jsonschema.
@santhosh-tekuri That's awesome, thank you!
Do you have any guidance on query parameters which are of type
net/url.Values (map[string][]string
) but could have a schema such as:
{
"parameters": [
{
"name": "limit",
"in": "query",
"description": "maximum number of results to return",
"required": false,
"schema": {
"type": "integer",
"format": "int32"
}
}
]
}
Setting aside the required
and in
, how would you advise handling the schema
? Perhaps an extension is needed for this?
Maybe using kin to validate just parameters makes more sense and then mapping their errors over to jsonschema proper errors.
from jsonschema.
if "schema"
is specified for a parameter, then its value must be valid json.
FYI:
- a simple integer
123
is a valid json - a boolean value
true
andfalse
is a valid json - quoted string
"hello"
is a valid json
so you can use jsonschema to validate. for example the following validates successfully:
schema := `{"type": "integer"}`
instance := 123
sch, err := jsonschema.CompileString("schema.json", schema)
if err != nil {
log.Fatalf("%#v", err)
}
if err = sch.Validate(instance); err != nil {
log.Fatalf("%#v", err)
}
from jsonschema.
Sorry to hijack this thread with that question.
schema := `{"type": "integer"}`
instance := "123"
sch, err := jsonschema.CompileString("schema.json", schema)
if err != nil {
log.Fatalf("%#v", err)
}
if err = sch.Validate(instance); err != nil {
log.Fatalf("%#v", err)
}
Doesn't fly though. I wasn't expecting your jsonschema package to handle that, I was just curious if you had any ideas on how to handle the potential for url.Values
.
I appreciate you taking the time to reply!
from jsonschema.
the code you posted works as expected giving the following error: expected integer, but got string
check in playground: https://go.dev/play/p/UI0_wpmgEN_x
from jsonschema.
Right, I know. The challenge is is that query strings always come in as map[string][]string
and have a getter method which will retrieve the value as a string
if it is a single value.
I wasn't sure if there was a mechanism to potentially treat strings as numbers but I think, ultimately, this is outside the purview of your project.
FWIW, I got partially through implementing a json schema implementation in a different language (rust) that supported up to 2020-12. It is, by no means, an easy feat. You did amazing work and I greatly appreciate your project. Thank you for it.
If my startup is successful, you'll be one of the first I'll help support. Thank you.
from jsonschema.
convert query value to string, number, boolean and validate each of them against the schema.
if any one of them validates then you can say value is valid against schema.
from jsonschema.
you can open new discussion from https://github.com/santhosh-tekuri/jsonschema/discussions, to discuss any rather then in issue
from jsonschema.
@chanced here is trivial implementation https://go.dev/play/p/hErh3XxSECW for validating query value
from jsonschema.
Related Issues (20)
- invalid jsonType: time.Time HOT 3
- Compiler short circuits when validating schemas, not providing the full list of errors
- Extension.Validate provides data in inconsistent order HOT 1
- Getting property name when compiling and validating user-defined keyword via extensions HOT 5
- Feature request: Pass custom compiler into "convenience" functions like CompileString HOT 2
- Performance drop when schema has "uniqueItems" set to true HOT 7
- `format` field of json schemas is not being validated HOT 1
- CompileString - stat .: permission denied HOT 1
- How can I validate to Python re flavor regex? HOT 4
- Proposal: Add Method to Generate Go Types from JSON Schema HOT 1
- Error when loading valid draft7 json schema HOT 2
- Validating json with json-schema HOT 4
- Draft2020 accepts invalid email address. HOT 5
- Support for $defs and definition keyword in Schema object. HOT 3
- ReadOnly and WriteOnly are getting ignored HOT 7
- i18n support
- Detailed output missing info
- Compiling a subresource with an extension validates the subresource against the root extension schema. HOT 6
- unexpected validation error which LSP is fine with HOT 10
- Provide CLI binaries with release HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonschema.