Skip to content

Commit 45389e9

Browse files
committed
Add default error message to is-youtube-url
1 parent ed02b66 commit 45389e9

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Keywords/is-youtube-url.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,22 @@ export default {
44
type: "string",
55
errors: true,
66
keyword: "is-youtube-url",
7-
validate: (_, value) => {
8-
return StringUtils.isValidYouTubeURL(value);
7+
compile: function compile() {
8+
return function validate(value) {
9+
validate.errors = null;
10+
11+
if (!StringUtils.isValidYouTubeURL(value)) {
12+
validate.errors = [
13+
{
14+
keyword: "is-youtube-url",
15+
message: "Invalid YouTube URL",
16+
params: { invalidInput: value },
17+
},
18+
];
19+
return false;
20+
}
21+
22+
return true;
23+
};
924
},
1025
};

src/Keywords/keywords.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe("Keywords", () => {
2222
type: "object",
2323
properties: { foo: { type: "string", "is-youtube-url": true } },
2424
};
25+
2526
const validate = ajv.compile(schema);
2627
const valid = validate({
2728
foo: "https://www.youtube.com/watch?v=uxQxd_z_uqc&t=24sd",
@@ -35,10 +36,14 @@ describe("Keywords", () => {
3536
type: "object",
3637
properties: { foo: { type: "string", "is-youtube-url": true } },
3738
};
39+
3840
const validate = ajv.compile(schema);
3941
const valid = validate({ foo: "https://example.com" });
4042

43+
const [error] = validate.errors;
44+
4145
expect(valid).toBe(false);
46+
expect(error.message).toBe("Invalid YouTube URL");
4247
});
4348
});
4449

0 commit comments

Comments
 (0)