Skip to content

Commit

Permalink
add example doc
Browse files Browse the repository at this point in the history
  • Loading branch information
candiduslynx committed Sep 26, 2023
1 parent 288322e commit 25fe25c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
42 changes: 42 additions & 0 deletions jsonschema/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jsonschema_test

import (
"fmt"
"log"

"github.com/cloudquery/codegen/jsonschema"
)

func Example_basicSchema() {
type basic struct {
A string `json:"a" jsonschema:"minLength=2,required"`
}

data, err := jsonschema.Generate(new(basic))
if err != nil {
log.Fatal(err)
}

fmt.Println(string(data))
// Output:
// {
// "$schema": "https://json-schema.org/draft/2020-12/schema",
// "$id": "https://github.com/cloudquery/codegen/jsonschema_test/basic",
// "$ref": "#/$defs/basic",
// "$defs": {
// "basic": {
// "properties": {
// "a": {
// "type": "string",
// "minLength": 2
// }
// },
// "additionalProperties": false,
// "type": "object",
// "required": [
// "a"
// ]
// }
// }
// }
}
1 change: 1 addition & 0 deletions jsonschema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/invopop/jsonschema"
)

// Generate returns a formatted JSON schema for the parameter.
func Generate(a any) ([]byte, error) {
sc := (&jsonschema.Reflector{RequiredFromJSONSchemaTags: true}).ReflectFromType(reflect.TypeOf(a))
return json.MarshalIndent(sc, "", " ")
Expand Down

0 comments on commit 25fe25c

Please sign in to comment.