forked from absoludity/goforms
/
fields.go
54 lines (45 loc) · 1.32 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
// Copyright 2012 The GoForms Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// Extended by MartinBrugnara (2014) <martin@martin-dev.eu>
//
/*
Package goforms/fields provides various types of fields for validating
and cleaning individual form values.
*/
package fields
import (
"errors"
)
// A field is able to clean a string value to the correct type
// or indicate a relevant error, as well as determine whether it is
// required.
type Field interface {
Clean(string) (interface{}, ValidationError)
CleanArray([]string) (interface{}, ValidationError)
IsRequired() bool
IsArray() bool
}
type ValidationError interface {
Error() string
}
// The types that are embedded/shared with more specific field types.
type BaseField struct {
Required bool
Array bool
}
// Returns whether data for this field is required.
func (f BaseField) IsRequired() bool {
return f.Required
}
// Returns true if this field support array.
func (f BaseField) IsArray() bool {
return f.Array
}
// Returns true if this field support array.
func (f BaseField) CleanArray([]string) (interface{}, ValidationError) {
return nil, ValidationError(errors.New("Not yet implemnted feature"))
}
// Specify the default attributes of a field.
type Defaults map[string]interface{}