Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gen): add minProperties/maxProperties support
- Loading branch information
Showing
7 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package validate | ||
|
||
import "github.com/go-faster/errors" | ||
|
||
// Object validates map length. | ||
type Object struct { | ||
MinProperties int | ||
MinPropertiesSet bool | ||
MaxProperties int | ||
MaxPropertiesSet bool | ||
} | ||
|
||
// SetMinProperties sets MinProperties validation. | ||
func (t *Object) SetMinProperties(v int) { | ||
t.MinPropertiesSet = true | ||
t.MinProperties = v | ||
} | ||
|
||
// SetMaxProperties sets MaxProperties validation. | ||
func (t *Object) SetMaxProperties(v int) { | ||
t.MaxPropertiesSet = true | ||
t.MaxProperties = v | ||
} | ||
|
||
// Set reports whether any validations are set. | ||
func (t Object) Set() bool { | ||
return t.MaxPropertiesSet || t.MinPropertiesSet | ||
} | ||
|
||
// ValidateProperties returns error if object length (properties number) v is invalid. | ||
func (t Object) ValidateProperties(v int) error { | ||
if t.MaxPropertiesSet && v > t.MaxProperties { | ||
return errors.Errorf("len %d greater than maximum %d", v, t.MaxProperties) | ||
} | ||
if t.MinPropertiesSet && v < t.MinProperties { | ||
return errors.Errorf("len %d less than minimum %d", v, t.MinProperties) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package validate | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestObject_Set(t *testing.T) { | ||
var v Object | ||
v.SetMaxProperties(10) | ||
v.SetMinProperties(5) | ||
require.True(t, v.Set()) | ||
require.Equal(t, Object{ | ||
MinProperties: 5, | ||
MinPropertiesSet: true, | ||
MaxProperties: 10, | ||
MaxPropertiesSet: true, | ||
}, v) | ||
require.NoError(t, v.ValidateProperties(7)) | ||
} | ||
|
||
func TestObject_ValidateProperties(t *testing.T) { | ||
for _, tc := range []struct { | ||
Name string | ||
Validator Object | ||
Value int | ||
Valid bool | ||
}{ | ||
{Name: "Zero", Valid: true}, | ||
{ | ||
Name: "MaxPropertiesOk", | ||
Validator: Object{MaxProperties: 10, MaxPropertiesSet: true}, | ||
Value: 5, | ||
Valid: true, | ||
}, | ||
{ | ||
Name: "MaxPropertiesErr", | ||
Validator: Object{MaxProperties: 10, MaxPropertiesSet: true}, | ||
Value: 15, | ||
Valid: false, | ||
}, | ||
{ | ||
Name: "MinPropertiesOk", | ||
Validator: Object{MinProperties: 10, MinPropertiesSet: true}, | ||
Value: 15, | ||
Valid: true, | ||
}, | ||
{ | ||
Name: "MinPropertiesErr", | ||
Validator: Object{MinProperties: 10, MinPropertiesSet: true}, | ||
Value: 5, | ||
Valid: false, | ||
}, | ||
{ | ||
Name: "BothOk", | ||
Validator: Object{ | ||
MinProperties: 10, | ||
MinPropertiesSet: true, | ||
MaxProperties: 15, | ||
MaxPropertiesSet: true, | ||
}, | ||
Value: 12, | ||
Valid: true, | ||
}, | ||
{ | ||
Name: "BothErrMax", | ||
Validator: Object{ | ||
MinProperties: 10, | ||
MinPropertiesSet: true, | ||
MaxProperties: 15, | ||
MaxPropertiesSet: true, | ||
}, | ||
Value: 17, | ||
Valid: false, | ||
}, | ||
{ | ||
Name: "BothErrMin", | ||
Validator: Object{ | ||
MinProperties: 10, | ||
MinPropertiesSet: true, | ||
MaxProperties: 15, | ||
MaxPropertiesSet: true, | ||
}, | ||
Value: 7, | ||
Valid: false, | ||
}, | ||
} { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
valid := tc.Validator.ValidateProperties(tc.Value) == nil | ||
assert.Equal(t, tc.Valid, valid, "max: %d, min: %d, v: %d", | ||
tc.Validator.MaxProperties, | ||
tc.Validator.MinProperties, | ||
tc.Value, | ||
) | ||
}) | ||
} | ||
} |