Giter Club home page Giter Club logo

Comments (15)

santhosh-tekuri avatar santhosh-tekuri commented on May 14, 2024 1

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.

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

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.

lkm avatar lkm commented on May 14, 2024

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.

lkm avatar lkm commented on May 14, 2024

Actually I've figured out a way to achieve this with extensions. Very nifty stuff.

from jsonschema.

fenollp avatar fenollp commented on May 14, 2024

@lkm Do you mind sharing your solution or at least the big picture of it?

from jsonschema.

lkm avatar lkm commented on May 14, 2024

@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.

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

@lkm you do not need to create extension for this. see here how to do this.

from jsonschema.

chanced avatar chanced commented on May 14, 2024

@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.

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

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 and false 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.

chanced avatar chanced commented on May 14, 2024

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.

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

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.

chanced avatar chanced commented on May 14, 2024

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.

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

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.

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

you can open new discussion from https://github.com/santhosh-tekuri/jsonschema/discussions, to discuss any rather then in issue

from jsonschema.

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

@chanced here is trivial implementation https://go.dev/play/p/hErh3XxSECW for validating query value

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.