ygot
ygot copied to clipboard
Handle custom validate method on fakeroot
Currently the Validate method called on the Go structures which are validated against the yang schema. However, there can be use cases where one need to have a custom validation which is not the part of yang schema.
Suggestion is to have a CustomValidateOptions as ValidateOptions which can be invoked in ytypes.Validate
.
@wenovus Can you please create a release? Thanks!
Created, thanks for the change.
Re-opening for a discussion.
I wonder whether we should implement this in a bit more general manner going forward -- perhaps similarly to how cmp
handles this. For example, we could have an API that is of the form:
type ValidatorFn func(interface{}) error
func WithCustomTypeValidator(t interface{}, fn ValidatorFn) { ... }
In this case, we'd compare the reflect.TypeOf
each element that we are validating against the types that are registered via WithCustomTypeValidator
- and fun the specified ValidatorFn
-- this will mean that a user can create some custom validation against an arbitrary container, or even leaf type if they wanted to (presuming it has its own specified type).
I don't think that we need to refactor this out, but it seems like we'll end up with two APIs - and it might be a good stage to end up with a more general purpose one here in the longer-term?
Sounds reasonable to me if there is a need for it from the community.
@robshakir Yes that would be the right approach as now the CustomValidator is invoked only on the fakeroot. It would be great if the validator func can be invoked on a container at least. Let me know if you have any specific design to fix this. I would be happy to contribute
PR #440 is the first step for this process