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

Value validation #11

Open
m33ch opened this issue Sep 6, 2019 · 3 comments
Open

Value validation #11

m33ch opened this issue Sep 6, 2019 · 3 comments

Comments

@m33ch
Copy link

m33ch commented Sep 6, 2019

Hi everyone, I'm trying to figure out how to implement a method to add custom validation.

Some idea?
Thanks a lot!

@ScreamZ
Copy link

ScreamZ commented Feb 18, 2022

+1 @alexeyraspopov Any idea

@alexeyraspopov
Copy link
Owner

I'm trying to figure out a way to define some sort of runtime value validation but it should be a complementary API. Right now you can write some sort of custom validation for cases where you need it:

class User extends Data {
  name: string = 'Anon';
  age: number = 0;

  static from(values) {
    let name: string = values.name ?? 'Anon'
    
    if (isNaN(values.age)) {
      throw new Error('age is missing')
    }
    
    let age: number = values.age;
    return User.create({ name, age })
  }
}

@ScreamZ
Copy link

ScreamZ commented Feb 23, 2022

Maybe we could implement a validate method that does nothing on the parent class, that you can override on the child class if you need validation and throw

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

3 participants