Skip to content

Commit

Permalink
feat: Add new keyword to check max items based on space type (strateg…
Browse files Browse the repository at this point in the history
…ies) (#951)

* feat: Add new keyword to check max length based on space type

* max items with space

* v0.10.0
  • Loading branch information
ChaituVR authored Jan 8, 2024
1 parent dc84213 commit 5f710df
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snapshot-labs/snapshot.js",
"version": "0.9.9",
"version": "0.10.0",
"repository": "snapshot-labs/snapshot.js",
"license": "MIT",
"main": "dist/snapshot.cjs.js",
Expand Down
6 changes: 5 additions & 1 deletion src/schemas/space.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@
"strategies": {
"type": "array",
"minItems": 1,
"maxItems": 8,
"maxItemsWithSpaceType": {
"default": 8,
"turbo": 10
},
"uniqueItems": true,
"items": {
"type": "object",
"properties": {
Expand Down
22 changes: 22 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ ajv.addKeyword({
errors: true
});

ajv.addKeyword({
keyword: 'maxItemsWithSpaceType',
validate: function validate(schema, data) {
// @ts-ignore
const spaceType = this.spaceType || 'default';
const isValid = data.length <= schema[spaceType];
if (!isValid) {
// @ts-ignore
validate.errors = [
{
keyword: 'maxItemsWithSpaceType',
message: `must NOT have more than ${schema[spaceType]} items`,
params: { limit: schema[spaceType] }
}
];
}
return isValid;
},
errors: true
});


// Custom URL format to allow empty string values
// https://github.com/snapshot-labs/snapshot.js/pull/541/files
ajv.addFormat('customUrl', {
Expand Down
11 changes: 11 additions & 0 deletions test/examples/space-maxItemsWithSpaceType-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"instancePath": "/strategies",
"keyword": "maxItemsWithSpaceType",
"message": "must NOT have more than 8 items",
"params": {
"limit": 8
},
"schemaPath": "#/properties/strategies/maxItemsWithSpaceType"
}
]
95 changes: 95 additions & 0 deletions test/examples/space-turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"name": "Loot Owners",
"skin": "indexed",
"about": "",
"admins": [
"0xF296178d553C8Ec21A2fBD2c5dDa8CA9ac905A00"
],
"avatar": "https://pbs.twimg.com/profile_images/1431587138202701826/lpgblc4h_400x400.jpg",
"github": "lootproject",
"symbol": "LOOT",
"filters": {
"minScore": 1,
"onlyMembers": false
},
"members": [],
"network": "1",
"plugins": {},
"twitter": "lootproject",
"strategies": [
{
"name": "erc721",
"params": {
"symbol": "LOOT",
"address": "0xff9c1b15b16263c61d017ee9f65c50e4ae0113d7"
}
},
{
"name": "erc721",
"params": {
"symbol": "LOOT",
"address": "0x0e42acBD23FAee03249DAFF896b78d7e79fBD58E"
}
},
{
"name": "erc721",
"params": {
"symbol": "LOOT",
"address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
}
},
{
"name": "erc721",
"params": {
"symbol": "LOOT",
"address": "0x6c3f90f043a72fa612cbac8115ee7e52bde6e490"
}
},
{
"name": "erc721",
"params": {
"symbol": "LOOT",
"address": "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"
}
},
{
"name": "erc721",
"params": {
"symbol": "LOOT",
"address": "0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9"
}
},
{
"name": "erc721",
"params": {
"symbol": "LOOT",
"address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
}
},
{
"name": "erc721",
"params": {
"symbol": "LOOT",
"address": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599"
}
},
{
"name": "erc721",
"params": {
"symbol": "LOOT",
"address": "0xbbbbca6a901c926f240b89eacb641d8aec7aeafd"
}
},
{
"name": "erc721",
"params": {
"symbol": "LOOT",
"address": "0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f"
}
}
],
"validation": {
"name": "basic",
"params": {}
}
}
12 changes: 10 additions & 2 deletions test/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { test, expect, describe } from 'vitest';
import { validateSchema } from '../src/utils';
import space from './examples/space.json';
import proposal from './examples/proposal.json';
import spaceTurbo from './examples/space-turbo.json';
import proposalTurbo from './examples/proposal-turbo.json';
import vote from './examples/vote.json';
import profile from './examples/profile.json';
import statement from './examples/statement.json';
import alias from './examples/alias.json';
import schemas from '../src/schemas';
import proposalMaxLengthWithSpaceTypeError from './examples/proposal-maxLengthWithSpaceType-error.json';
import spaceMaxItemsWithSpaceTypeError from './examples/space-maxItemsWithSpaceType-error.json';


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

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

describe.each([
// { schemaType: 'space', schema: schemas.space, example: space },
{
schemaType: 'space',
schema: schemas.space,
example: spaceTurbo,
error: spaceMaxItemsWithSpaceTypeError
},
{
schemaType: 'proposal',
schema: schemas.proposal,
Expand Down

0 comments on commit 5f710df

Please sign in to comment.