[#872] changed the schema required validator to be optional for auth collections

This commit is contained in:
Gani Georgiev
2022-11-16 15:13:04 +02:00
parent 4528f075dc
commit 6e9cf986c5
45 changed files with 1166 additions and 445 deletions
+4 -7
View File
@@ -133,13 +133,9 @@ func (s *Schema) AddField(newField *SchemaField) {
// Internally calls each individual field's validator and additionally
// checks for invalid renamed fields and field name duplications.
func (s Schema) Validate() error {
return validation.Validate(&s.fields, validation.Required, validation.By(func(value any) error {
return validation.Validate(&s.fields, validation.By(func(value any) error {
fields := s.fields // use directly the schema value to avoid unnecessary interface casting
if len(fields) == 0 {
return validation.NewError("validation_invalid_schema", "Invalid schema format.")
}
ids := []string{}
names := []string{}
for i, field := range fields {
@@ -204,8 +200,9 @@ func (s *Schema) UnmarshalJSON(data []byte) error {
// Value implements the [driver.Valuer] interface.
func (s Schema) Value() (driver.Value, error) {
if len(s.fields) == 0 {
return nil, nil
if s.fields == nil {
// initialize an empty slice to ensure that `[]` is returned
s.fields = []*SchemaField{}
}
data, err := json.Marshal(s.fields)
+2 -2
View File
@@ -215,7 +215,7 @@ func TestSchemaValidate(t *testing.T) {
// no fields
{
schema.NewSchema(),
true,
false,
},
// duplicated field ids
{
@@ -342,7 +342,7 @@ func TestSchemaValue(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if v1 != nil {
if v1 != "[]" {
t.Fatalf("Expected nil, got %v", v1)
}