Total Lines | 40 |
Duplicated Lines | 0 % |
Coverage | 87.5% |
Changes | 0 |
1 | package validation |
||
2 | |||
3 | // Option is used to set up validation process of a value. |
||
4 | type Option interface { |
||
5 | // SetUp is called when the validation process is initialized |
||
6 | // and can be used to gracefully handle errors when initializing constraints. |
||
7 | SetUp() error |
||
8 | } |
||
9 | |||
10 | // internalOption is used to tune the validation scope before starting the validation process. |
||
11 | type internalOption interface { |
||
12 | setUpOnScope(scope *Scope) error |
||
13 | } |
||
14 | |||
15 | // optionFunc is an adapter that allows to use ordinary functions as validation options. |
||
16 | type optionFunc func(scope *Scope) error |
||
17 | |||
18 | func (f optionFunc) SetUp() error { |
||
19 | return nil |
||
20 | } |
||
21 | |||
22 | func (f optionFunc) setUpOnScope(scope *Scope) error { |
||
23 | 1 | return f(scope) |
|
24 | } |
||
25 | |||
26 | // PropertyName option adds name of the given property to current validation scope. |
||
27 | func PropertyName(propertyName string) Option { |
||
28 | 1 | return optionFunc(func(scope *Scope) error { |
|
29 | 1 | scope.propertyPath = append(scope.propertyPath, PropertyNameElement(propertyName)) |
|
30 | |||
31 | 1 | return nil |
|
32 | }) |
||
33 | } |
||
34 | |||
35 | // ArrayIndex option adds index of the given array to current validation scope. |
||
36 | func ArrayIndex(index int) Option { |
||
37 | 1 | return optionFunc(func(scope *Scope) error { |
|
38 | 1 | scope.propertyPath = append(scope.propertyPath, ArrayIndexElement(index)) |
|
39 | |||
40 | 1 | return nil |
|
41 | }) |
||
43 |