ygot icon indicating copy to clipboard operation
ygot copied to clipboard

Handle custom validate method on fakeroot

Open soumiksamanta opened this issue 4 years ago • 6 comments

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.

soumiksamanta avatar Aug 31 '20 20:08 soumiksamanta

@wenovus Can you please create a release? Thanks!

soumiksamanta avatar Sep 01 '20 23:09 soumiksamanta

Created, thanks for the change.

wenovus avatar Sep 01 '20 23:09 wenovus

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?

robshakir avatar Sep 02 '20 20:09 robshakir

Sounds reasonable to me if there is a need for it from the community.

wenovus avatar Sep 03 '20 15:09 wenovus

@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

soumiksamanta avatar Sep 03 '20 23:09 soumiksamanta

PR #440 is the first step for this process

soumiksamanta avatar Sep 04 '20 01:09 soumiksamanta