-
Notifications
You must be signed in to change notification settings - Fork 1
/
forms.go
113 lines (94 loc) · 2.94 KB
/
forms.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
package forms
import (
"fmt"
jtd "github.com/jsontypedef/json-typedef-go"
)
// Form is the generic interface for forms.
type Form interface {
ID() string
Type() string
GetSchema() *jtd.Schema
GetAdditionalBackends() []Backend
AddBackend(Backend) error
Validate(Response) bool
}
// StructuredForm is a Form with a defined schema. Submitted data must match the given schema
type StructuredForm struct {
id string
Schema *jtd.Schema
AdditionalBackends []Backend
}
// NewStructuredForm creates a new structured form with a given ID and schema
func NewStructuredForm(id string, schema *jtd.Schema, backends ...Backend) *StructuredForm {
return &StructuredForm{
id: id,
Schema: schema,
AdditionalBackends: backends,
}
}
// ID returns the structured form ID
func (f *StructuredForm) ID() string {
return f.id
}
// Type returns the type of the form (Structured)
func (f *StructuredForm) Type() string {
return "Structured"
}
// GetSchema returns the data schema of the form
func (f *StructuredForm) GetSchema() *jtd.Schema {
return f.Schema
}
// GetAdditionalBackends returns the additional backends for this form
func (f *StructuredForm) GetAdditionalBackends() []Backend {
return f.AdditionalBackends
}
// AddBackend adds a new backend to the form
func (f *StructuredForm) AddBackend(b Backend) error {
f.AdditionalBackends = append(f.AdditionalBackends, b)
return nil
}
// Validate validates that the response follows the form's schema
func (f *StructuredForm) Validate(response Response) bool {
if errs, _ := jtd.Validate(*f.Schema, response); errs != nil && len(errs) > 0 {
fmt.Println(errs)
return false
}
return true
}
// UnstructuredForm is a form without any given schema. It accepts unstructured data
type UnstructuredForm struct {
id string
AdditionalBackends []Backend
}
// NewUnstructuredForm creates a new unstructured form with a given id
func NewUnstructuredForm(id string, backends ...Backend) *UnstructuredForm {
return &UnstructuredForm{
id: id,
AdditionalBackends: backends,
}
}
// ID returns the ID of the unstructured form
func (f *UnstructuredForm) ID() string {
return f.id
}
// Type returns the type of the form (Unstructured)
func (f *UnstructuredForm) Type() string {
return "Unstructured"
}
// GetSchema returns nil because unstructured form do not have schemas
func (f *UnstructuredForm) GetSchema() *jtd.Schema {
return nil
}
// GetAdditionalBackends returns the additional backends for this form
func (f *UnstructuredForm) GetAdditionalBackends() []Backend {
return f.AdditionalBackends
}
// AddBackend adds a new backend to the form
func (f *UnstructuredForm) AddBackend(b Backend) error {
f.AdditionalBackends = append(f.AdditionalBackends, b)
return nil
}
// Validate validates that the response follows the form's schema : always true as unstructured form
func (f *UnstructuredForm) Validate(response Response) bool {
return true
}