Skip to content

Commit 3c1d755

Browse files
committed
Export individual keywords and create instance inside the test
1 parent 04d9d28 commit 3c1d755

6 files changed

Lines changed: 45 additions & 42 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@programmer_network/ajv",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A JSON Schema Validator",
55
"author": "Aleksandar Grbic - (https://programmer.network)",
66
"publishConfig": {

src/Keywords/get-bad-words.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import StringUtils from "../Utils/StringUtils";
22
import { KeywordDefinition, SchemaValidateFunction } from "ajv";
33
import { DataValidateFunction } from "ajv/dist/types";
44

5-
const keywordName = "getBadWords";
5+
const keywordName = "disallow-profanity";
66

77
const keyword: KeywordDefinition = {
88
type: "string",

src/Keywords/has-text.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import TiptapUtils from "../Utils/TiptapUtils/index.js";
33
import { KeywordDefinition, SchemaValidateFunction } from "ajv";
44
import { DataValidateFunction } from "ajv/dist/types/index.js";
55

6+
const keywordName = "has-text";
7+
68
const validate: SchemaValidateFunction | DataValidateFunction = (
79
schema: any,
810
input: any
@@ -12,7 +14,7 @@ const validate: SchemaValidateFunction | DataValidateFunction = (
1214
if (!data) {
1315
validate.errors = [
1416
{
15-
keyword: "has-text",
17+
keyword: keywordName,
1618
message: "The value must contain text"
1719
}
1820
];
@@ -29,7 +31,7 @@ const validate: SchemaValidateFunction | DataValidateFunction = (
2931
if (!isNotEmpty) {
3032
validate.errors = [
3133
{
32-
keyword: "has-text",
34+
keyword: keywordName,
3335
message: "The value must contain text"
3436
}
3537
];

src/Keywords/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ import hasText from "./has-text.js";
33
import secureString from "./is-secure-string.js";
44
import validYouTubeUrl from "./is-youtube-url.js";
55

6+
export { getBadWords, hasText, secureString, validYouTubeUrl };
7+
68
export const keywords = [hasText, getBadWords, validYouTubeUrl, secureString];

src/Keywords/keywords.test.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
import { ajv } from "..";
1+
import { keywords } from ".";
2+
import Ajv from "ajv";
3+
import addCustomMessages from "ajv-errors";
4+
import addFormats from "ajv-formats";
5+
6+
const ajv = addFormats(
7+
addCustomMessages(
8+
new Ajv({ allErrors: true, $data: true, removeAdditional: true })
9+
)
10+
);
11+
12+
keywords.map(keyword => ajv.addKeyword(keyword));
213

314
describe("Keywords", () => {
415
describe("getBadWords", () => {
5-
test("should fail if getBadWords returns bad words", () => {
16+
it("should fail if getBadWords returns bad words", () => {
617
const schema = {
718
type: "object",
8-
properties: { foo: { type: "string", getBadWords: false } }
19+
properties: { foo: { type: "string", "disallow-profanity": false } }
920
};
1021
const validate = ajv.compile(schema);
1122
validate({ foo: "hi penis" });
@@ -16,7 +27,7 @@ describe("Keywords", () => {
1627
});
1728

1829
describe("is-youtube-url", () => {
19-
test("should pass if url is valid", () => {
30+
it("should pass if url is valid", () => {
2031
const schema = {
2132
type: "object",
2233
properties: { foo: { type: "string", "is-youtube-url": true } }
@@ -30,7 +41,7 @@ describe("Keywords", () => {
3041
expect(valid).toBe(true);
3142
});
3243

33-
test("should fail if url is invalid", () => {
44+
it("should fail if url is invalid", () => {
3445
const schema = {
3546
type: "object",
3647
properties: { foo: { type: "string", "is-youtube-url": true } }
@@ -43,7 +54,7 @@ describe("Keywords", () => {
4354
expect(validate?.errors?.[0].message).toBe("Invalid YouTube URL");
4455
});
4556

46-
test("should pass if url is falsy but the minLength is 0 or not present", () => {
57+
it("should pass if url is falsy but the minLength is 0 or not present", () => {
4758
const schema = {
4859
type: "object",
4960
properties: {
@@ -57,7 +68,7 @@ describe("Keywords", () => {
5768
expect(valid).toBe(true);
5869
});
5970

60-
test("should fail if url is falsy value but the minLength equal or greater then 1", () => {
71+
it("should fail if url is falsy value but the minLength equal or greater then 1", () => {
6172
const schema = {
6273
type: "object",
6374
properties: {
@@ -73,7 +84,7 @@ describe("Keywords", () => {
7384
});
7485

7586
describe("has-text", () => {
76-
test("should pass if the text has at least 5 characters", () => {
87+
it("should pass if the text has at least 5 characters", () => {
7788
const schema = {
7889
type: "object",
7990
properties: {
@@ -92,7 +103,7 @@ describe("Keywords", () => {
92103
expect(valid).toBe(true);
93104
});
94105

95-
test("should fail if the text doesn't have at least 5 characters", () => {
106+
it("should fail if the text doesn't have at least 5 characters", () => {
96107
const schema = {
97108
type: "object",
98109
properties: {
@@ -114,7 +125,7 @@ describe("Keywords", () => {
114125
);
115126
});
116127

117-
test("should fail if the has more then 5 characters", () => {
128+
it("should fail if the has more then 5 characters", () => {
118129
const schema = {
119130
type: "object",
120131
properties: {
@@ -136,7 +147,7 @@ describe("Keywords", () => {
136147
);
137148
});
138149

139-
test("should fail if there's no text", () => {
150+
it("should fail if there's no text", () => {
140151
const schema = {
141152
type: "object",
142153
properties: {
@@ -156,7 +167,7 @@ describe("Keywords", () => {
156167
expect(validate?.errors?.[0].message).toBe("The value must contain text");
157168
});
158169

159-
test("should pass if the text contains a valid YouTube URL", () => {
170+
it("should pass if the text contains a valid YouTube URL", () => {
160171
const schema = {
161172
type: "object",
162173
properties: {
@@ -192,7 +203,7 @@ describe("Keywords", () => {
192203
});
193204

194205
describe("secure-string", () => {
195-
test("should pass if the string is empty", () => {
206+
it("should pass if the string is empty", () => {
196207
const schema = {
197208
type: "object",
198209
properties: {
@@ -211,7 +222,7 @@ describe("Keywords", () => {
211222
expect(valid).toBe(true);
212223
});
213224

214-
test("should pass if the string is secure", () => {
225+
it("should pass if the string is secure", () => {
215226
const schema = {
216227
type: "object",
217228
properties: {
@@ -230,7 +241,7 @@ describe("Keywords", () => {
230241
expect(valid).toBe(true);
231242
});
232243

233-
test("should fail if the string contains dissallowed characters", () => {
244+
it("should fail if the string contains dissallowed characters", () => {
234245
const schema = {
235246
type: "object",
236247
properties: {
@@ -252,7 +263,7 @@ describe("Keywords", () => {
252263
);
253264
});
254265

255-
test("should fail if the string contains dissallowed characters", () => {
266+
it("should fail if the string contains dissallowed characters", () => {
256267
const schema = {
257268
type: "object",
258269
properties: {
@@ -274,7 +285,7 @@ describe("Keywords", () => {
274285
);
275286
});
276287

277-
test("should fail if the string contains profanity", () => {
288+
it("should fail if the string contains profanity", () => {
278289
const schema = {
279290
type: "object",
280291
properties: {
@@ -296,7 +307,7 @@ describe("Keywords", () => {
296307
);
297308
});
298309

299-
test("should fail if the string only contains space characters", () => {
310+
it("should fail if the string only contains space characters", () => {
300311
const schema = {
301312
type: "object",
302313
properties: {
@@ -318,7 +329,7 @@ describe("Keywords", () => {
318329
);
319330
});
320331

321-
test("should fail if the string only contains space characters", () => {
332+
it("should fail if the string only contains space characters", () => {
322333
const schema = {
323334
type: "object",
324335
properties: {
@@ -340,7 +351,7 @@ describe("Keywords", () => {
340351
);
341352
});
342353

343-
test("should fail if the string contains unicode characters", () => {
354+
it("should fail if the string contains unicode characters", () => {
344355
const schema = {
345356
type: "object",
346357
properties: {
@@ -362,7 +373,7 @@ describe("Keywords", () => {
362373
);
363374
});
364375

365-
test("should fail if the string contains combined characters", () => {
376+
it("should fail if the string contains combined characters", () => {
366377
const schema = {
367378
type: "object",
368379
properties: {
@@ -384,7 +395,7 @@ describe("Keywords", () => {
384395
);
385396
});
386397

387-
test("should fail in the correct order", () => {
398+
it("should fail in the correct order", () => {
388399
const schema = {
389400
type: "object",
390401
properties: {

src/index.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
import { keywords } from "./Keywords/index.js";
2-
import Ajv from "ajv";
3-
import addCustomMessages from "ajv-errors";
4-
import addFormats from "ajv-formats";
5-
6-
export const ajv = addFormats(
7-
addCustomMessages(
8-
new Ajv({ allErrors: true, $data: true, removeAdditional: true })
9-
)
10-
);
11-
12-
keywords.map(keyword => ajv.addKeyword(keyword));
13-
14-
export default {
15-
keywords
16-
};
1+
export { default as getBadWords } from "./Keywords/get-bad-words";
2+
export { default as hasText } from "./Keywords/has-text";
3+
export { default as secureString } from "./Keywords/is-secure-string";
4+
export { default as validYouTubeUrl } from "./Keywords/is-youtube-url";

0 commit comments

Comments
 (0)