Skip to content

Commit 5f710df

Browse files
authored
feat: Add new keyword to check max items based on space type (strategies) (#951)
* feat: Add new keyword to check max length based on space type * max items with space * v0.10.0
1 parent dc84213 commit 5f710df

File tree

6 files changed

+144
-4
lines changed

6 files changed

+144
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@snapshot-labs/snapshot.js",
3-
"version": "0.9.9",
3+
"version": "0.10.0",
44
"repository": "snapshot-labs/snapshot.js",
55
"license": "MIT",
66
"main": "dist/snapshot.cjs.js",

src/schemas/space.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@
103103
"strategies": {
104104
"type": "array",
105105
"minItems": 1,
106-
"maxItems": 8,
106+
"maxItemsWithSpaceType": {
107+
"default": 8,
108+
"turbo": 10
109+
},
110+
"uniqueItems": true,
107111
"items": {
108112
"type": "object",
109113
"properties": {

src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@ ajv.addKeyword({
113113
errors: true
114114
});
115115

116+
ajv.addKeyword({
117+
keyword: 'maxItemsWithSpaceType',
118+
validate: function validate(schema, data) {
119+
// @ts-ignore
120+
const spaceType = this.spaceType || 'default';
121+
const isValid = data.length <= schema[spaceType];
122+
if (!isValid) {
123+
// @ts-ignore
124+
validate.errors = [
125+
{
126+
keyword: 'maxItemsWithSpaceType',
127+
message: `must NOT have more than ${schema[spaceType]} items`,
128+
params: { limit: schema[spaceType] }
129+
}
130+
];
131+
}
132+
return isValid;
133+
},
134+
errors: true
135+
});
136+
137+
116138
// Custom URL format to allow empty string values
117139
// https://github.com/snapshot-labs/snapshot.js/pull/541/files
118140
ajv.addFormat('customUrl', {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"instancePath": "/strategies",
4+
"keyword": "maxItemsWithSpaceType",
5+
"message": "must NOT have more than 8 items",
6+
"params": {
7+
"limit": 8
8+
},
9+
"schemaPath": "#/properties/strategies/maxItemsWithSpaceType"
10+
}
11+
]

test/examples/space-turbo.json

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"name": "Loot Owners",
3+
"skin": "indexed",
4+
"about": "",
5+
"admins": [
6+
"0xF296178d553C8Ec21A2fBD2c5dDa8CA9ac905A00"
7+
],
8+
"avatar": "https://pbs.twimg.com/profile_images/1431587138202701826/lpgblc4h_400x400.jpg",
9+
"github": "lootproject",
10+
"symbol": "LOOT",
11+
"filters": {
12+
"minScore": 1,
13+
"onlyMembers": false
14+
},
15+
"members": [],
16+
"network": "1",
17+
"plugins": {},
18+
"twitter": "lootproject",
19+
"strategies": [
20+
{
21+
"name": "erc721",
22+
"params": {
23+
"symbol": "LOOT",
24+
"address": "0xff9c1b15b16263c61d017ee9f65c50e4ae0113d7"
25+
}
26+
},
27+
{
28+
"name": "erc721",
29+
"params": {
30+
"symbol": "LOOT",
31+
"address": "0x0e42acBD23FAee03249DAFF896b78d7e79fBD58E"
32+
}
33+
},
34+
{
35+
"name": "erc721",
36+
"params": {
37+
"symbol": "LOOT",
38+
"address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
39+
}
40+
},
41+
{
42+
"name": "erc721",
43+
"params": {
44+
"symbol": "LOOT",
45+
"address": "0x6c3f90f043a72fa612cbac8115ee7e52bde6e490"
46+
}
47+
},
48+
{
49+
"name": "erc721",
50+
"params": {
51+
"symbol": "LOOT",
52+
"address": "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"
53+
}
54+
},
55+
{
56+
"name": "erc721",
57+
"params": {
58+
"symbol": "LOOT",
59+
"address": "0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9"
60+
}
61+
},
62+
{
63+
"name": "erc721",
64+
"params": {
65+
"symbol": "LOOT",
66+
"address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
67+
}
68+
},
69+
{
70+
"name": "erc721",
71+
"params": {
72+
"symbol": "LOOT",
73+
"address": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599"
74+
}
75+
},
76+
{
77+
"name": "erc721",
78+
"params": {
79+
"symbol": "LOOT",
80+
"address": "0xbbbbca6a901c926f240b89eacb641d8aec7aeafd"
81+
}
82+
},
83+
{
84+
"name": "erc721",
85+
"params": {
86+
"symbol": "LOOT",
87+
"address": "0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f"
88+
}
89+
}
90+
],
91+
"validation": {
92+
"name": "basic",
93+
"params": {}
94+
}
95+
}

test/schema.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import { test, expect, describe } from 'vitest';
22
import { validateSchema } from '../src/utils';
33
import space from './examples/space.json';
44
import proposal from './examples/proposal.json';
5+
import spaceTurbo from './examples/space-turbo.json';
56
import proposalTurbo from './examples/proposal-turbo.json';
67
import vote from './examples/vote.json';
78
import profile from './examples/profile.json';
89
import statement from './examples/statement.json';
910
import alias from './examples/alias.json';
1011
import schemas from '../src/schemas';
1112
import proposalMaxLengthWithSpaceTypeError from './examples/proposal-maxLengthWithSpaceType-error.json';
13+
import spaceMaxItemsWithSpaceTypeError from './examples/space-maxItemsWithSpaceType-error.json';
14+
1215

1316
describe.each([
1417
{ schemaType: 'space', schema: schemas.space, example: space },
@@ -30,7 +33,7 @@ describe.each([
3033
// Tests for turbo spaces
3134

3235
describe.each([
33-
{ schemaType: 'space', schema: schemas.space, example: space },
36+
{ schemaType: 'space', schema: schemas.space, example: spaceTurbo },
3437
{ schemaType: 'proposal', schema: schemas.proposal, example: proposalTurbo }
3538
])(`Run validate for turbo spaces`, ({ schemaType, schema, example }) => {
3639
test(`validating schema ${schemaType} should return true`, () => {
@@ -43,7 +46,12 @@ describe.each([
4346
});
4447

4548
describe.each([
46-
// { schemaType: 'space', schema: schemas.space, example: space },
49+
{
50+
schemaType: 'space',
51+
schema: schemas.space,
52+
example: spaceTurbo,
53+
error: spaceMaxItemsWithSpaceTypeError
54+
},
4755
{
4856
schemaType: 'proposal',
4957
schema: schemas.proposal,

0 commit comments

Comments
 (0)