Skip to content

Commit

Permalink
fix: render schema should require version and support latest (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Mar 22, 2022
1 parent c82e462 commit 48a4e7a
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 63 deletions.
3 changes: 2 additions & 1 deletion cmd/dev/release/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package release
import (
"bytes"
"fmt"
"github.com/Masterminds/semver/v3"
"os"
"regexp"
"strings"

"github.com/Masterminds/semver/v3"

"github.com/spf13/cobra"

"github.com/ory/x/flagx"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{
"$id": "https://github.com/ory/cli/cmd/dev/schema/fixtures/render_version_test/.schema/version.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Test Fixture schema.",
"type": "object",
"oneOf": [
{
"allOf": [
{
"properties": {
"version": {
"const": "v0.0.0"
"const": "v1.0.0"
}
}
},
"required": [
"version"
]
},
{
"$ref": "https://raw.githubusercontent.com/ory/hydra/v0.0.0/.schema/config.schema.json"
"$ref": "https://raw.githubusercontent.com/ory/hydra/v1.0.0/.schema/config.schema.json"
}
]
},
Expand All @@ -23,14 +24,51 @@
{
"properties": {
"version": {
"const": "v1.0.0"
"const": "v0.0.0"
}
}
},
"required": [
"version"
]
},
{
"$ref": "https://raw.githubusercontent.com/ory/hydra/v1.0.0/.schema/config.schema.json"
"$ref": "https://raw.githubusercontent.com/ory/hydra/v0.0.0/.schema/config.schema.json"
}
]
},
{
"allOf": [
{
"oneOf": [
{
"properties": {
"version": {
"type": "string",
"maxLength": 0
}
},
"required": [
"version"
]
},
{
"not": {
"properties": {
"version": {}
},
"required": [
"version"
]
}
}
]
},
{
"$ref": "#/oneOf/0/allOf/1"
}
]
}
]
}
],
"title": "Test Fixture schema.",
"type": "object"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,47 @@
"version": {
"const": "v0.0.0"
}
}
},
"required": [
"version"
]
},
{
"$ref": "https://raw.githubusercontent.com/ory/hydra/v0.0.0/.schema/config.schema.json"
}
]
},
{
"allOf": [
{
"oneOf": [
{
"properties": {
"version": {
"type": "string",
"maxLength": 0
}
},
"required": [
"version"
]
},
{
"not": {
"properties": {
"version": {}
},
"required": [
"version"
]
}
}
]
},
{
"$ref": "#/oneOf/0/allOf/1"
}
]
}
]
}
32 changes: 32 additions & 0 deletions cmd/dev/schema/latest.fragment.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"allOf": [
{
"oneOf": [
{
"properties": {
"version": {
"type": "string",
"maxLength": 0
}
},
"required": [
"version"
]
},
{
"not": {
"properties": {
"version": {}
},
"required": [
"version"
]
}
}
]
},
{
"$ref": "#/oneOf/0/allOf/1"
}
]
}
36 changes: 25 additions & 11 deletions cmd/dev/schema/render_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/ory/jsonschema/v3/httploader"
"github.com/ory/x/httpx"
"io/ioutil"
"os"
"path"
"regexp"
"strings"

"github.com/ory/cli/spec"
"github.com/ory/jsonschema/v3/httploader"
"github.com/ory/x/httpx"

"github.com/ory/x/jsonschemax"

"github.com/spf13/cobra"
"github.com/tidwall/sjson"

"github.com/ory/cli/cmd/pkg"
"github.com/ory/jsonschema/v3"
Expand Down Expand Up @@ -53,32 +52,47 @@ func addVersionToSchema(cmd *cobra.Command, args []string) {
"version": {
"const": "%s"
}
}
},
"required": [
"version"
]
},
{
"$ref": "%s"
}
]
}`, newVersion, ref)

versionSchema, err := ioutil.ReadFile(destFile)
f, err := os.Open(destFile)
pkg.Check(err)
defer f.Close()

var versionSchema map[string]json.RawMessage
pkg.Check(json.NewDecoder(f).Decode(&versionSchema))

renderedVersionSchema, err := sjson.SetBytes(versionSchema, "oneOf.-1", json.RawMessage(newVersionEntry))
var oneOf []json.RawMessage
if err := json.Unmarshal(versionSchema["oneOf"], &oneOf); err != nil {
pkg.Check(err)
}

// prepend the newest entry
oneOf = append([]json.RawMessage{json.RawMessage(newVersionEntry)}, oneOf...)

versionSchema["oneOf"], err = json.Marshal(oneOf)
pkg.Check(err)

var prettyVersionSchema bytes.Buffer
pkg.Check(json.Indent(&prettyVersionSchema, renderedVersionSchema, "", strings.Repeat(" ", 4)))
prettyVersionSchema, err := json.MarshalIndent(versionSchema, "", strings.Repeat(" ", 4))
pkg.Check(err)

ctx := context.WithValue(cmd.Context(), httploader.ContextKey, httpx.NewResilientClient())
schema, err := jsonschema.CompileString(ctx, "version_meta.schema.json", string(spec.VersionSchema))
pkg.Check(err)

err = schema.Validate(bytes.NewBuffer(prettyVersionSchema.Bytes()))
err = schema.Validate(bytes.NewReader(prettyVersionSchema))
if err != nil {
jsonschemax.FormatValidationErrorForCLI(os.Stderr, prettyVersionSchema.Bytes(), err)
jsonschemax.FormatValidationErrorForCLI(os.Stderr, prettyVersionSchema, err)
os.Exit(1)
}

pkg.Check(ioutil.WriteFile(destFile, prettyVersionSchema.Bytes(), 0600))
pkg.Check(ioutil.WriteFile(destFile, prettyVersionSchema, 0600))
}
8 changes: 5 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package cmd
import (
"context"
"fmt"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/ory/cli/buildinfo"
"github.com/ory/x/cloudx"
"github.com/ory/x/cmdx"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"os"
)

func NewRootCmd() *cobra.Command {
Expand Down
Loading

0 comments on commit 48a4e7a

Please sign in to comment.