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

How to cast an Object to a model defined with this utility? #1

Closed
fxdave opened this issue Feb 21, 2020 · 1 comment
Closed

How to cast an Object to a model defined with this utility? #1

fxdave opened this issue Feb 21, 2020 · 1 comment

Comments

@fxdave
Copy link

fxdave commented Feb 21, 2020

I used Joi to cast the input objects into well-defined models.
This package has a Validate function which reads the schema from its parameter.
It's like a bit nonsense since typescript doesn't allow creating non-valid objects (at least for the types).

So there should be something like this:

const obj = {
    something: '12'
}

class Model {
     @Required()
     something: number,
     @Required()
     somethingElse: string
}

try {
    const model : Model = Validate(Model, obj)
} catch(e) {
    console.error("Invalid object given")
}

Thanks

@elisvathi
Copy link
Owner

@fxdave you are right, i was currently using the validation explicitly by first getting the schema using the constructor and then validating it with Joi directly (same thing the Validate function does).
const schema = getSchema(Model); return Joi.validate(obj, schema);
The validate function was there for objects created with the constructor. Now its fixed on a5c3785

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

2 participants