-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
jsonschema
helper package (#28)
Part of cloudquery/cloudquery#13877
- Loading branch information
1 parent
6b74ef4
commit 636eada
Showing
4 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
// ] | ||
// } | ||
// } | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
|
||
"github.com/invopop/jsonschema" | ||
) | ||
|
||
// Generate returns a formatted JSON schema for the input struct, according to the tags | ||
// defined by https://github.com/invopop/jsonschema | ||
func Generate(a any) ([]byte, error) { | ||
sc := (&jsonschema.Reflector{RequiredFromJSONSchemaTags: true}).ReflectFromType(reflect.TypeOf(a)) | ||
return json.MarshalIndent(sc, "", " ") | ||
} |