joiful-ts / joiful

TypeScript Declarative Validation for Joi
239 stars 18 forks source link

Feature Request: Export getSchema, hasSchema functions #107

Closed codeandcats closed 4 years ago

codeandcats commented 4 years ago

Summary

Motivation

@stepanzabelin wanted a way to tell if a class has a schema associated with it (see #105). This PR addresses this issue by exporting a hasSchema function that returns whether a class has a schema or not.

Ticket

Closes #105.

codeandcats commented 4 years ago

If/once we're happy for this to go in I'll follow it up with a new release.

stepanzabelin commented 4 years ago

:fire::fire::fire: Thank you @codeandcats

laurence-myers commented 4 years ago

Just wondering, does it make sense for validation to pass when validating against a class that has no validation schema/metadata? To me, this suggests a bug, like someone forgot to define the validation schema.

codeandcats commented 4 years ago

That's a fair question @laurence-myers.

I think I prefer the idea of it passing validation as then it doesn't require the consumer to have an extra step to check if it has a schema or not.

But I don't have a strong opinion on it and happy to change it. So would you say you are leaning towards it throwing an error (like "No schema found for class" or something)?

stepanzabelin commented 4 years ago

I would rather get joiful's error and resolve it in the development process but getJoiSchema function is needed

codeandcats commented 4 years ago

@stepanzabelin Okay that makes sense. 👍

@laurence-myers if you’re happy I’ll make the necessary changes tomorrow night (bedtime now) and follow up with a publish.

codeandcats commented 4 years ago

Okay, I've changed the default behaviour of the validator functions (validate, validateAsClass, validateArrayAsClass) so that they will error (with NoValidationSchemaForClassError) when trying to validate against a class that has no schema associated with it. This will be a breaking change and will trigger a major version bump when releasing.

I've also added a new convenience function hasSchema(Class): boolean

CC: @stepanzabelin.

Can you please re-review @laurence-myers?

stepanzabelin commented 4 years ago

Hi there. I am looking forward to a new release 🔥

codeandcats commented 4 years ago

@stepanzabelin Sorry for the hold up. I've just merged and published version 2.0.0. 😄

stepanzabelin commented 4 years ago

@codeandcats many thanks I've installed and tested. Everything is fine!