Skip to content

6.1.2

Compare
Choose a tag to compare
@Josuto Josuto released this 22 Nov 17:54
· 10 commits to main since this release

What's Changed

Have you ever wanted to know all the invalid fields of an entity instance(*) before it is stored in the database at once? Now you can! 💃🎁🕺

You can simply capture the ValidationException error on a try-catch block surrounding the entity save operation and invoke any of the following three new functions:

  • getInvalidFields retrieves the names of all the entity fields specifying an invalid value
  • getInvalidRequiredFields retrieves the names of all the mandatory entity fields with no defined value
  • getInvalidUniqueFields retrieves the names of all the entity fields which values are meant to be unique in your collection

Code example: 🧑‍💻

const book = new Book({
    title: undefined, // say this field is specified as mandatory in your book Mongoose Schema
    isbn: "978-0-123456-47-2" // say this field is specified as unique but there is another stored book with this value
});
try {
    bookRepository.save(book);
} catch (error) {
    if (error instanceof ValidationError) {
        console.error(error.getInvalidFields()) // 'title', 'isbn'
        console.error(error.getInvalidRequiredFields()) // 'title'
        console.error(error.getInvalidUniqueFields()) // 'isbn'
    } else {
        throw error;
    }
}

Hope you find this new feature useful! 😙👋

(*) By invalid entity instance fields we refer to those fields of the instance which values break any constraint you have imposed on them in your entity schema.

Pull Requests

  • feat: add validation errors to validation exception cause by @Josuto in #325
  • fix: rename validation exception functions by @Josuto in #326

Full Changelog: 6.0.2...6.1.2