-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: add validation for birthdate * fix: change error code for birthdate validation * fix: wrong place of BirthDate validation * feat: add tests for BirthDate validation * refactor: move tests to another location * fix: error in description of tests * feat: add cross-component validation test for BirthDate * refactor: use Luxon for isInLeapYear function * refactor: use Luxon for validation of BirthDate * chore: bump version of content and runtime * refactor: use inline Error message instead of ValidationError * feat: add ValidationErrorWithoutProperty class * fix: add validation to prevent the creation of BirthDates in the future * refactor: move tests from runtime to content * refactor: move files back --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
422e358
commit 5242e14
Showing
6 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ValidationError } from "@js-soft/ts-serval"; | ||
|
||
export class ValidationErrorWithoutProperty extends ValidationError { | ||
public constructor(type: string, reason: string, cause?: Error) { | ||
super(type, "n/a", reason, cause); | ||
|
||
this.message = `${type} :: ${reason}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { UserfriendlyApplicationError } from "@nmshd/app-runtime/src/UserfriendlyApplicationError"; | ||
import { BirthDate } from "@nmshd/content"; | ||
import { ValidationErrorWithoutProperty } from "@nmshd/content/src/ValidationErrorWithoutProperty"; | ||
import { DateTime } from "luxon"; | ||
|
||
describe("creation of RepositoryAttributes of Attribute Value Type BirthDate", () => { | ||
test("can create a RepositoryAttribute of Attribute Value Type BirthDate", function () { | ||
const validBirthDate = BirthDate.from({ day: 1, month: 12, year: 1990 }); | ||
expect(validBirthDate.constructor.name).toBe("BirthDate"); | ||
expect(validBirthDate.day.value).toBe(1); | ||
expect(validBirthDate.month.value).toBe(12); | ||
expect(validBirthDate.year.value).toBe(1990); | ||
}); | ||
|
||
test("returns an error when trying to create an invalid BirthDate with violated validation criteria of a single property", function () { | ||
const invalidBirthDateCall = () => { | ||
BirthDate.from({ day: 1, month: 13, year: 1990 }); | ||
}; | ||
expect(invalidBirthDateCall).toThrow( | ||
new UserfriendlyApplicationError("error.runtime.requestDeserialization", "BirthMonth.value:Number :: must be an integer value between 1 and 12") | ||
); | ||
}); | ||
|
||
test("returns an error when trying to create an invalid BirthDate with cross-component violated validation criteria for June", function () { | ||
const invalidBirthDateCall = () => { | ||
BirthDate.from({ day: 31, month: 6, year: 1990 }); | ||
}; | ||
expect(invalidBirthDateCall).toThrow(new ValidationErrorWithoutProperty(BirthDate.name, "The BirthDate is not a valid date.")); | ||
}); | ||
|
||
test("returns an error when trying to create an invalid BirthDate with cross-component violated validation criteria for February", function () { | ||
const invalidBirthDateCall = () => { | ||
BirthDate.from({ day: 29, month: 2, year: 2010 }); | ||
}; | ||
expect(invalidBirthDateCall).toThrow(new ValidationErrorWithoutProperty(BirthDate.name, "The BirthDate is not a valid date.")); | ||
}); | ||
|
||
test("returns an error when trying to create an BirthDate that is in the future", function () { | ||
const currentDateTime = DateTime.utc(); | ||
const yearInFuture = currentDateTime.year + 1; | ||
|
||
const invalidBirthDateCall = () => { | ||
BirthDate.from({ day: 10, month: 6, year: yearInFuture }); | ||
}; | ||
expect(invalidBirthDateCall).toThrow(new ValidationErrorWithoutProperty(BirthDate.name, "You cannot enter a BirthDate that is in the future.")); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters