Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple ElemMatch from schema.Array struct #276

Open
mihailquantive opened this issue Mar 30, 2023 · 0 comments
Open

Decouple ElemMatch from schema.Array struct #276

mihailquantive opened this issue Mar 30, 2023 · 0 comments

Comments

@mihailquantive
Copy link

I'm trying to create my custom FieldValidator implementation that I want to use for fields that are actually arrays, but I have some issues when I try to use the $elemMatch operator on fields of that type.

The problem is that Prepare method of ElemMatch tries to cast field's validator to *schema.Array but my field is not of that type so the method returns an error:

func (e *ElemMatch) Prepare(validator schema.Validator) error {
	f, err := getValidatorField(e.Field, validator)
	if err != nil {
		return err
	}

	arr, ok := f.Validator.(*schema.Array)
	if !ok {
		return fmt.Errorf("%s: is not an array", e.Field)
	}

	// FIXME: Should allow any type.
	obj, ok := arr.Values.Validator.(*schema.Object)
	if !ok {
		return fmt.Errorf("%s: array elements are not schema.Object", e.Field)
	}

	return prepareExpressions(e.Exps, obj.Schema)
}

Can we somehow decouple the implementation of this method from the schema.Array struct? I was thinking of adding an interface that will look something like this:

type ArrayValidator interface { GetElementValidator() (Validator, error) }

... and cast the validator to that interface instead and then call the GetElementValidator()
What do you thing about that? If you like it I can open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant