Skip to content

Add a check for Required (with solution/code provided) #7

@tg-mvooges

Description

@tg-mvooges

Currently when applying this code, every property gets reported back as missing wether its marked as required or not. I could make my own filter to fix that, but adding a simple isRequired property it would make this a lot easier.

I've come up with a backwards compatible way to still report all missing fields, but also giving the developer the option to decide wether or not to do something with the error:

type FieldMissing struct {
	/* Other fields here */ 	

	// IsRequired indicates whether or not the field is marked as required in the structs tags.
	IsRequired boolean
}
type FieldMismatch struct {
	/* Other fields here */ 	

	// IsRequired indicates whether or not the field is marked as required in the structs tags.
	IsRequired boolean
}
func contains(s []string, str string) bool {
	for _, v := range s {
		if v == str {
			return true
		}
	}

	return false
}

func fieldIsRequired(f reflect.StructField) (bool) {
	tag := f.Tag.Get("validate")
	if tag == "" {
		return false;
	}
	
	return contains(strings.Split(tags, ","), "required")
}

And then apply them in schema.go, eg:

missing := FieldMissing{Field: fieldName, IsRequired: fieldIsRequired(field)}
mismatch := FieldMismatch{ IsRequired: fieldIsRequired(field), /* */}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions