-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add command to generate JSON schema of config (dc) (#240)
* Add command to generate JSON schema of config * minor niggles --------- Co-authored-by: kaiiorg <k349@live.com>
- Loading branch information
1 parent
04d4698
commit 0b5e020
Showing
5 changed files
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/invopop/jsonschema" | ||
) | ||
|
||
var ( | ||
ErrGeneratedSchemaIsNil = errors.New("generated JSON Schema is nil") | ||
) | ||
|
||
func JSONSchema() ([]byte, error) { | ||
schema := jsonschema.Reflect(&Config{}) | ||
|
||
if schema == nil { | ||
return nil, ErrGeneratedSchemaIsNil | ||
} | ||
|
||
return schema.MarshalJSON() | ||
} |
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,18 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/invopop/jsonschema" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJSONSchema(t *testing.T) { | ||
schemaJSON, err := JSONSchema() | ||
assert.NoError(t, err) | ||
assert.NotNil(t, schemaJSON) | ||
|
||
unmarshalledSchema := &jsonschema.Schema{} | ||
err = unmarshalledSchema.UnmarshalJSON(schemaJSON) | ||
assert.NoError(t, err) | ||
} |
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