Skip to content

Commit 219020b

Browse files
committed
fix: delegate schema validation to settings
1 parent c4c7289 commit 219020b

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

src/writer/proposal.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,6 @@ async function validateSpace(originalSpace: any) {
5454
delete space.verified;
5555
delete space.id;
5656

57-
const schemaIsValid: any = snapshot.utils.validateSchema(snapshot.schemas.space, space, {
58-
snapshotEnv: SNAPSHOT_ENV
59-
});
60-
61-
if (schemaIsValid !== true) {
62-
const firstErrorObject: any = Object.values(schemaIsValid)[0];
63-
if (firstErrorObject.message === 'network not allowed') {
64-
return Promise.reject(firstErrorObject.message);
65-
}
66-
return Promise.reject('wrong space format');
67-
}
68-
6957
try {
7058
await validateSpaceSettings(space);
7159
} catch (e) {

src/writer/settings.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ const SNAPSHOT_ENV = process.env.NETWORK || 'testnet';
99
const broviderUrl = process.env.BROVIDER_URL || 'https://rpc.snapshot.org';
1010

1111
export async function validateSpaceSettings(space: any) {
12+
const schemaIsValid: any = snapshot.utils.validateSchema(snapshot.schemas.space, space, {
13+
snapshotEnv: SNAPSHOT_ENV
14+
});
15+
16+
if (schemaIsValid !== true) {
17+
log.warn('[writer] Wrong space format', schemaIsValid);
18+
const firstErrorObject: any = Object.values(schemaIsValid)[0];
19+
if (firstErrorObject.message === 'network not allowed') {
20+
return Promise.reject(firstErrorObject.message);
21+
}
22+
return Promise.reject('wrong space format');
23+
}
24+
1225
if (SNAPSHOT_ENV !== 'testnet') {
1326
const hasTicket = space.strategies.some(strategy => strategy.name === 'ticket');
1427
const hasVotingValidation =
@@ -31,24 +44,6 @@ export async function validateSpaceSettings(space: any) {
3144

3245
export async function verify(body): Promise<any> {
3346
const msg = jsonParse(body.msg);
34-
35-
const schemaIsValid: any = snapshot.utils.validateSchema(snapshot.schemas.space, msg.payload, {
36-
snapshotEnv: SNAPSHOT_ENV
37-
});
38-
39-
if (schemaIsValid !== true) {
40-
log.warn('[writer] Wrong space format', schemaIsValid);
41-
const firstErrorObject: any = Object.values(schemaIsValid)[0];
42-
if (firstErrorObject.message === 'network not allowed') {
43-
return Promise.reject(firstErrorObject.message);
44-
}
45-
return Promise.reject('wrong space format');
46-
}
47-
48-
const controller = await snapshot.utils.getSpaceController(msg.space, DEFAULT_NETWORK, {
49-
broviderUrl
50-
});
51-
const isController = controller === body.address;
5247
const space = await getSpace(msg.space, true);
5348

5449
try {
@@ -57,6 +52,11 @@ export async function verify(body): Promise<any> {
5752
return Promise.reject(e);
5853
}
5954

55+
const controller = await snapshot.utils.getSpaceController(msg.space, DEFAULT_NETWORK, {
56+
broviderUrl
57+
});
58+
const isController = controller === body.address;
59+
6060
if (space?.deleted) return Promise.reject('space deleted, contact admin');
6161
const admins = (space?.admins || []).map(admin => admin.toLowerCase());
6262
const isAdmin = admins.includes(body.address.toLowerCase());

0 commit comments

Comments
 (0)