Skip to content

Move FieldValidator Validate() methods to be be on a pointer value #110

@smyrman

Description

@smyrman

We use quite some documentation-real estate to explain that schema.FeildValidators should be set using pointers so that any (potential) schema.Compiler interface (maybe renamed after #100 ) can be detected and called. THere is also code catching this mis-usage and giving a runtime error.

However, it's generally better to fail faster, and it would be pretty easy to make this a compile-time error by moving the Validate method for most FieldValidator implementations to be on a pointer value:

/ String validates string based values
type String struct {
	...
}

// Compile compiles and validate regexp if any.
func (v *String) Compile() (err error) {
	...
}

// Validate validates and normalize string based value.
func (v *String) Validate(value interface{}) (interface{}, error) {
	...
}

This also means that the run-time checks can be removed!

PS! Some types, such as maps and slices, do not require neither Compiler nor Validate to be on a pointer value due to how these types work.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions