-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from koyopro/feature/validations
[accel-record] Added an interface to define validations for the Model as static properties
- Loading branch information
Showing
9 changed files
with
193 additions
and
56 deletions.
There are no files selected for viewing
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,6 @@ | ||
--- | ||
"accel-record-core": minor | ||
"accel-record": minor | ||
--- | ||
|
||
Added an interface to define validations for the Model as static properties |
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
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
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 @@ | ||
export * from "accel-record-core/dist/model/validations.js"; |
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,17 @@ | ||
import { validates } from "accel-record/validations"; | ||
import { ValidateSampleModel } from "../validateSample"; | ||
|
||
test("validates()", () => { | ||
validates(ValidateSampleModel, [["accepted", { acceptance: true }]]); | ||
validates(ValidateSampleModel, [[["key", "size"], { presence: true }]]); | ||
validates(ValidateSampleModel, [["accepted", { acceptance: true }]]); | ||
validates(ValidateSampleModel, [["key", { uniqueness: { scope: ["size", "id"] } }]]); | ||
// @ts-expect-error | ||
validates(ValidateSampleModel, [["foo", { acceptance: true }]]); | ||
// @ts-expect-error | ||
validates(ValidateSampleModel, [[["key", "foo"], { presence: true }]]); | ||
// @ts-expect-error | ||
validates(ValidateSampleModel, [["accepted", { foo: true }]]); | ||
// @ts-expect-error | ||
validates(ValidateSampleModel, [["key", { uniqueness: { scope: ["size", "foo"] } }]]); | ||
}); |
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,24 @@ | ||
import { FormModel } from "accel-record"; | ||
import { attributes } from "accel-record/attributes"; | ||
import { validates } from "accel-record/validations"; | ||
|
||
class SampleForm extends FormModel { | ||
static validations = validates(this, [ | ||
[ | ||
"count", | ||
{ | ||
numericality: { between: [0, 10] }, | ||
}, | ||
], | ||
]); | ||
|
||
count = attributes.integer(0); | ||
} | ||
|
||
test(".validations", () => { | ||
const form = SampleForm.build({ count: 5 }); | ||
expect(form.isValid()).toBe(true); | ||
form.count = 11; | ||
expect(form.isValid()).toBe(false); | ||
expect(form.errors.fullMessages).toEqual(["Count must be between 0 and 10"]); | ||
}); |
Oops, something went wrong.