-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhas-text.ts
More file actions
75 lines (61 loc) · 1.61 KB
/
has-text.ts
File metadata and controls
75 lines (61 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import StringUtils from "../Utils/StringUtils/index.js";
import TiptapUtils from "../Utils/TiptapUtils/index.js";
import { KeywordDefinition, SchemaValidateFunction } from "ajv";
import { DataValidateFunction } from "ajv/dist/types/index.js";
const keywordName = "has-text";
const validate: SchemaValidateFunction | DataValidateFunction = (
schema: any,
input: any
) => {
const { data } = StringUtils.safeJSONParse(input);
if (!data) {
validate.errors = [
{
keyword: keywordName,
message: "The value must contain text"
}
];
return false;
}
if (TiptapUtils.containsYouTubeVideo(data.content)) {
return true;
}
const { isNotEmpty, length } = TiptapUtils.hasText(data);
if (!isNotEmpty) {
validate.errors = [
{
keyword: keywordName,
message: "The value must contain text"
}
];
return false;
}
if (schema.minLength && length < schema.minLength) {
validate.errors = [
{
keyword: "has-text",
message: `The text must have a length greater than or equal to ${schema.minLength} characters`,
params: { minLength: schema.minLength }
}
];
return false;
}
if (schema.maxLength && length > schema.maxLength) {
validate.errors = [
{
keyword: "has-text",
message: `The length of the text cannot exceed ${schema.maxLength} characters`,
params: { maxLength: schema.maxLength }
}
];
return false;
}
return true;
};
const keyword: KeywordDefinition = {
type: "string",
errors: true,
keyword: "has-text",
validate
};
export default keyword;