-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
fields.go
159 lines (133 loc) · 3.15 KB
/
fields.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package form
import (
"encoding/json"
"fmt"
"strings"
"github.com/ory/x/jsonschemax"
"github.com/ory/kratos/schema"
"github.com/ory/kratos/text"
)
const DisableFormField = "disableFormField"
// Fields contains multiple fields
//
// swagger:model formFields
type Fields []Field
// Field represents a HTML Form Field
//
// swagger:model formField
type Field struct {
// Name is the equivalent of `<input name="{{.Name}}">`
//
// required: true
Name string `json:"name"`
// Type is the equivalent of `<input type="{{.Type}}">`
//
// enum:
// - hidden
// - email
// - password
// - date
// - url
// - text
// - datetime-local
// - number
// - submit
// required: true
Type string `json:"type"`
// Pattern is the equivalent of `<input pattern="{{.Pattern}}">`
Pattern string `json:"pattern,omitempty"`
// Disabled is the equivalent of `<input {{if .Disabled}}disabled{{end}}">`
Disabled bool `json:"disabled,omitempty"`
// Required is the equivalent of `<input required="{{.Required}}">`
Required bool `json:"required,omitempty"`
// Value is the equivalent of `<input value="{{.Value}}">`
Value interface{} `json:"value,omitempty" faker:"string"`
// Messages contains a list of messages (e.g. validation errors) that affect this field.
Messages text.Messages `json:"messages,omitempty"`
}
// Reset resets a field's value and errors.
func (f *Field) Reset() {
f.Messages = nil
f.Value = nil
}
func (ff *Fields) sortBySchema(schemaRef, prefix string) (func(i, j int) bool, error) {
schemaKeys, err := schema.GetKeysInOrder(schemaRef)
if err != nil {
return nil, err
}
keysInOrder := []string{
CSRFTokenName,
"identifier",
"password",
}
for _, k := range schemaKeys {
if prefix != "" {
k = fmt.Sprintf("%s.%s", prefix, k)
}
keysInOrder = append(keysInOrder, k)
}
getKeyPosition := func(name string) int {
lastPrefix := len(keysInOrder)
for i, n := range keysInOrder {
if strings.HasPrefix(name, n) {
lastPrefix = i
}
}
return lastPrefix
}
return func(i, j int) bool {
return getKeyPosition((*ff)[i].Name) < getKeyPosition((*ff)[j].Name)
}, nil
}
func toFormType(n string, i interface{}) string {
switch n {
case CSRFTokenName:
return "hidden"
case "password":
return "password"
}
switch i.(type) {
case float64, int64, int32, float32, json.Number:
return "number"
case bool:
return "checkbox"
}
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()
}
// Set disabled if the custom property is set
if isDisabled, ok := p.CustomProperties[DisableFormField]; ok {
if isDisabled, ok := isDisabled.(bool); ok {
f.Disabled = isDisabled
}
}
return f
}
func addPrefix(name, prefix, separator string) string {
if prefix == "" {
return name
}
return fmt.Sprintf("%s%s%s", prefix, separator, name)
}