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

Custom validation for custom class #421

Open
mariusrak opened this issue Dec 12, 2020 · 2 comments
Open

Custom validation for custom class #421

mariusrak opened this issue Dec 12, 2020 · 2 comments

Comments

@mariusrak
Copy link

It is possible to use a class in a type definition. But since it is not a blackbox, it will be validate. While an instance has all the properties it should have, they are not described in a schema and hence produce errors. But describing whole schema for every used class is not a good practice. It leads at least to duplication of code but also violates encapsulation. I'd expect some class-level approach.

It would be nice if classes could expose function that would be used for validation. Also utility.isObjectWeShouldTraverse should return false if the schema type is custom class, validation should only check whether the value is of type of the class. The value should be then validated only by custom provided function. Also it should not be necessary to state that is is blackbox if custom validation function is provided.

The class defined validation function could be static and instance methods. So e.g.

static validate(){}
validate(){}

Anyways: currently it is not clear from documentation how should be custom class type definition treated.

@github-actions
Copy link

Thank you for submitting an issue!

If this is a bug report, please be sure to include, at minimum, example code showing a small schema and any necessary calls with all their arguments, which will reproduce the issue. Even better, you can link to a saved online code editor example, where anyone can immediately run the code and see the issue.

If you are requesting a feature, include a code example of how you imagine it working if it were implemented.

If you need to edit your issue description, click the [...] and choose Edit.

Be patient. This is a free and freely licensed package that I maintain in my spare time. You may get a response in a day, but it could also take a month. If you benefit from this package and would like to see more of my time devoted to it, you can help by sponsoring.

@aldeed
Copy link
Collaborator

aldeed commented Dec 14, 2020

The approach has been to validate class instances as well as possible, but there hasn't been a lot of thought put into customizing that validation. My thinking is that people may want to validate instances of classes they don't own, so it would be best to support this in the schema definition. It might look something like this:

{
  type: SomeClass,
  toPlainObject(obj) {
    return {
      foo: obj.foo(),
      bar: obj.bar,
      // etc.
    };
  }
}

Or if a class did expose a method, then you'd do:

{
  type: SomeClass,
  toPlainObject(obj) {
    return obj.toPlainObject();
  }
}

This would need some experimentation to see what works, but I'm willing to look at a PR if someone wants to attempt it.

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

No branches or pull requests

2 participants