Skip to content

Commit

Permalink
Improve field types (#209)
Browse files Browse the repository at this point in the history
Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
  • Loading branch information
zepatrik and aeneasr authored Jan 31, 2020
1 parent fc32207 commit aeefa93
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 5 deletions.
8 changes: 8 additions & 0 deletions docs/api.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,10 @@
"description": "Field represents a HTML Form Field",
"type": "object",
"properties": {
"disabled": {
"description": "Disabled is the equivalent of \u003cinput disabled=\"{{.Disabled}}\"\u003e",
"type": "string"
},
"errors": {
"description": "Errors contains all validation errors this particular field has caused.",
"type": "array",
Expand All @@ -768,6 +772,10 @@
"description": "Name is the equivalent of \u003cinput name=\"{{.Name}}\"\u003e",
"type": "string"
},
"pattern": {
"description": "Pattern is the equivalent of \u003cinput pattern=\"{{.Pattern}}\"\u003e",
"type": "string"
},
"required": {
"description": "Required is the equivalent of \u003cinput required=\"{{.Required}}\"\u003e",
"type": "boolean"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ require (
github.com/ory/gojsonschema v1.2.0
github.com/ory/graceful v0.1.1
github.com/ory/herodot v0.6.3
github.com/ory/sdk/swagutil v0.0.0-20200130190227-e1f6d26486c5
github.com/ory/sdk/swagutil v0.0.0-20200131083057-53fe3c2ddf8a
github.com/ory/viper v1.5.6
github.com/ory/x v0.0.92
github.com/pelletier/go-toml v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,8 @@ github.com/ory/sdk/swagutil v0.0.0-20200130171837-e84d4a4046c8 h1:LBkYajX6JmhlRk
github.com/ory/sdk/swagutil v0.0.0-20200130171837-e84d4a4046c8/go.mod h1:Ufg1eAyz+Zt3+oweSZVThG13ewewWCKwBmoNmK8Z0co=
github.com/ory/sdk/swagutil v0.0.0-20200130190227-e1f6d26486c5 h1:sqkb7LZ5dg+zgDLjj/akeqYihllVb10SJ2cwN+hl89U=
github.com/ory/sdk/swagutil v0.0.0-20200130190227-e1f6d26486c5/go.mod h1:Ufg1eAyz+Zt3+oweSZVThG13ewewWCKwBmoNmK8Z0co=
github.com/ory/sdk/swagutil v0.0.0-20200131083057-53fe3c2ddf8a h1:bXsrzEDh1BACRHydHOhvhfxJ8b4W/cPMMbTQlwvWRyE=
github.com/ory/sdk/swagutil v0.0.0-20200131083057-53fe3c2ddf8a/go.mod h1:Ufg1eAyz+Zt3+oweSZVThG13ewewWCKwBmoNmK8Z0co=
github.com/ory/viper v1.5.6 h1:w4ceGgWwWLzAFYQ7bHaDZmwNsAto2JPVdyQjQnn7VWI=
github.com/ory/viper v1.5.6/go.mod h1:TYmpFpKLxjQwvT4f0QPpkOn4sDXU1kDgAwJpgLYiQ28=
github.com/ory/x v0.0.84 h1:foL2GSeL9yXBsEU14WcdX95H4Z0TmyodAJwdWkzvtHI=
Expand Down
6 changes: 6 additions & 0 deletions internal/httpclient/models/form_field.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions internal/httpclient/models/login_request_method.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions selfservice/form/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"strings"

"github.com/ory/x/jsonschemax"

"github.com/ory/kratos/schema"
)

Expand All @@ -21,6 +23,10 @@ type Field struct {
Name string `json:"name"`
// Type is the equivalent of <input type="{{.Type}}">
Type string `json:"type,omitempty"`
// Pattern is the equivalent of <input pattern="{{.Pattern}}">
Pattern string `json:"pattern,omitempty"`
// Disabled is the equivalent of <input disabled="{{.Disabled}}">
Disabled string `json:"disabled,omitempty"`
// Required is the equivalent of <input required="{{.Required}}">
Required bool `json:"required,omitempty"`
// Value is the equivalent of <input value="{{.Value}}">
Expand Down Expand Up @@ -82,6 +88,36 @@ func toFormType(n string, i interface{}) string {
return "text"
}

func fieldFromPath(name string, p jsonschemax.Path) Field {
f := Field{
Name: name,
Type: "text",
}

// Estimating type
f.Type = toFormType(p.Name, p.Type)

switch p.Format {
case "date-time":
f.Type = "datetime-local"
case "email":
f.Type = "email"
case "date":
f.Type = "date"
case "uri":
f.Type = "url"
case "regex":
f.Type = "text"
}

// Other properties
if p.Pattern != nil {
f.Pattern = p.Pattern.String()
}

return f
}

func addPrefix(name, prefix, separator string) string {
if prefix == "" {
return name
Expand Down
34 changes: 34 additions & 0 deletions selfservice/form/fields_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
package form

import (
"bytes"
"fmt"
"io/ioutil"
"testing"

"github.com/santhosh-tekuri/jsonschema/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"

"github.com/ory/x/jsonschemax"
)

func TestFieldFromPath(t *testing.T) {
t.Run("all properties are properly transfered", func(t *testing.T) {
schema, err := ioutil.ReadFile("./stub/all_formats.schema.json")
require.NoError(t, err)

c := jsonschema.NewCompiler()
require.NoError(t, c.AddResource("test.json", bytes.NewBuffer(schema)))

paths, err := jsonschemax.ListPaths("test.json", c)
require.NoError(t, err)

for _, path := range paths {
htmlField := fieldFromPath(path.Name, path)
assert.Equal(t, gjson.GetBytes(schema, fmt.Sprintf("properties.%s.test_expected_type", path.Name)).String(), htmlField.Type)
assert.True(t, !gjson.GetBytes(schema, fmt.Sprintf("properties.%s.test_expected_pattern", path.Name)).Exists() || (gjson.GetBytes(schema, fmt.Sprintf("properties.%s.test_expected_pattern", path.Name)).Bool() && htmlField.Pattern != ""))
fmt.Printf("name %s\ntype %s\n", htmlField.Name, htmlField.Type)
}
})
}

//
// func TestNewFormFieldsFromJSON(t *testing.T) {
// var js = json.RawMessage(`{"numby":1.5,"stringy":"foobar","objy":{"objy":{},"numby":1.5,"stringy":"foobar"}}`)
Expand Down
5 changes: 1 addition & 4 deletions selfservice/form/html_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ func NewHTMLFormFromJSONSchema(action, jsonSchemaRef, prefix string) (*HTMLForm,
c := NewHTMLForm(action)
for _, value := range paths {
name := addPrefix(value.Name, prefix, ".")
c.Fields = append(c.Fields, Field{
Name: name,
Type: toFormType(value.Name, value.Type),
})
c.Fields = append(c.Fields, fieldFromPath(name, value))
}

return c, nil
Expand Down
58 changes: 58 additions & 0 deletions selfservice/form/stub/all_formats.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"$id": "https://example.com/all_formats.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"password": {
"type": "string",
"test_expected_type": "password"
},
"csrf_token": {
"type": "string",
"test_expected_type": "hidden"
},
"simpleString": {
"type": "string",
"test_expected_type": "text"
},
"simpleNumber": {
"type": "number",
"test_expected_type": "number"
},
"simpleBoolean": {
"type": "boolean",
"test_expected_type": "checkbox"
},
"emailString": {
"type": "string",
"format": "email",
"test_expected_type": "email"
},
"dateTimeString": {
"type": "string",
"format": "date-time",
"test_expected_type": "datetime-local"
},
"regexString": {
"type": "string",
"format": "regex",
"test_expected_type": "text"
},
"dateString": {
"type": "string",
"format": "date",
"test_expected_type": "date"
},
"uriString": {
"type": "string",
"format": "uri",
"test_expected_type": "url"
},
"patternString": {
"type": "string",
"pattern": "foo.*",
"test_expected_type": "text",
"test_expected_pattern": true
}
}
}

0 comments on commit aeefa93

Please sign in to comment.