Skip to content

Commit b54cdf7

Browse files
authored
feat: Add new keyword to check max length based on space type (#949)
1 parent 29383ae commit b54cdf7

File tree

5 files changed

+99
-4
lines changed

5 files changed

+99
-4
lines changed

src/schemas/proposal.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
"type": "string",
1717
"title": "body",
1818
"minLength": 0,
19-
"maxLength": 20000
19+
"maxLengthWithSpaceType": {
20+
"default": 10000,
21+
"turbo": 20000
22+
}
2023
},
2124
"discussion": {
2225
"type": "string",

src/utils.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ ajv.addKeyword({
9292
}
9393
});
9494

95+
ajv.addKeyword({
96+
keyword: 'maxLengthWithSpaceType',
97+
validate: function validate(schema, data) {
98+
// @ts-ignore
99+
const spaceType = this.spaceType || 'default';
100+
const isValid = data.length <= schema[spaceType];
101+
if (!isValid) {
102+
// @ts-ignore
103+
validate.errors = [
104+
{
105+
keyword: 'maxLengthWithSpaceType',
106+
message: `must NOT have more than ${schema[spaceType]} characters`,
107+
params: { limit: schema[spaceType] }
108+
}
109+
];
110+
}
111+
return isValid;
112+
},
113+
errors: true
114+
});
115+
95116
// Custom URL format to allow empty string values
96117
// https://github.com/snapshot-labs/snapshot.js/pull/541/files
97118
ajv.addFormat('customUrl', {
@@ -423,11 +444,17 @@ export async function validate(
423444
}
424445
}
425446

447+
interface validateSchemaOptions {
448+
snapshotEnv?: string;
449+
spaceType?: string;
450+
}
451+
426452
export function validateSchema(
427453
schema,
428454
data,
429-
options = {
430-
snapshotEnv: 'default'
455+
options: validateSchemaOptions = {
456+
snapshotEnv: 'default',
457+
spaceType: 'default'
431458
}
432459
) {
433460
const ajvValidate = ajv.compile(schema);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"instancePath": "/body",
4+
"keyword": "maxLengthWithSpaceType",
5+
"message": "must NOT have more than 10000 characters",
6+
"params": {
7+
"limit": 10000
8+
},
9+
"schemaPath": "#/properties/body/maxLengthWithSpaceType"
10+
}
11+
]

0 commit comments

Comments
 (0)