Skip to content

Commit

Permalink
feat: Check if network is mainnet or testnet depending on env (#936)
Browse files Browse the repository at this point in the history
* feat: Check if network is mainnet or testnet depending on env

* v0.9.0
  • Loading branch information
ChaituVR authored Nov 16, 2023
1 parent 6be303e commit 7d30d83
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 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.8.3",
"version": "0.9.0",
"repository": "snapshot-labs/snapshot.js",
"license": "MIT",
"main": "dist/snapshot.cjs.js",
Expand Down
5 changes: 4 additions & 1 deletion src/schemas/space.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
},
"network": {
"type": "string",
"snapshotNetwork": true,
"title": "network",
"minLength": 1,
"maxLength": 32
Expand Down Expand Up @@ -114,7 +115,8 @@
"network": {
"type": "string",
"maxLength": 12,
"title": "network"
"title": "network",
"snapshotNetwork": true
},
"params": {
"type": "object",
Expand Down Expand Up @@ -346,6 +348,7 @@
},
"network": {
"type": "string",
"snapshotNetwork": true,
"title": "Network",
"maxLength": 12
}
Expand Down
3 changes: 2 additions & 1 deletion src/schemas/zodiac.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"properties": {
"network": {
"title": "Network",
"type": "string"
"type": "string",
"snapshotNetwork": true
},
"multisend": {
"title": "Multisend contract address",
Expand Down
39 changes: 36 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ const scoreApiHeaders = {
'Content-Type': 'application/json'
};

const ajv = new Ajv({ allErrors: true, allowUnionTypes: true, $data: true });
const ajv = new Ajv({
allErrors: true,
allowUnionTypes: true,
$data: true,
passContext: true
});
// @ts-ignore
addFormats(ajv);

Expand Down Expand Up @@ -68,6 +73,28 @@ ajv.addFormat('ethValue', {
}
});

const networksIds = Object.keys(networks);
const mainnetNetworkIds = Object.keys(networks).filter(
(id) => !networks[id].testnet
);
const testnetNetworkIds = Object.keys(networks).filter(
(id) => networks[id].testnet
);

ajv.addKeyword({
keyword: 'snapshotNetwork',
validate: function (schema, data) {
// @ts-ignore
const snapshotEnv = this.snapshotEnv || 'default';
if (snapshotEnv === 'mainnet') return mainnetNetworkIds.includes(data);
if (snapshotEnv === 'testnet') return testnetNetworkIds.includes(data);
return networksIds.includes(data);
},
error: {
message: 'must be a valid network used by snapshot'
}
});

// Custom URL format to allow empty string values
// https://github.com/snapshot-labs/snapshot.js/pull/541/files
ajv.addFormat('customUrl', {
Expand Down Expand Up @@ -338,9 +365,15 @@ export async function validate(
}
}

export function validateSchema(schema, data) {
export function validateSchema(
schema,
data,
options = {
snapshotEnv: 'default'
}
) {
const ajvValidate = ajv.compile(schema);
const valid = ajvValidate(data);
const valid = ajvValidate.call(options, data);
return valid ? valid : ajvValidate.errors;
}

Expand Down
2 changes: 1 addition & 1 deletion test/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe.each([
{ schemaType: 'alias', schema: schemas.alias, example: alias }
])(`Run validate for all schemas`, ({ schemaType, schema, example }) => {
test(`validating schema ${schemaType} should return true`, () => {
const isValid = validateSchema(schema, example);
const isValid = validateSchema(schema, example, { snapshotEnv: 'mainnet' });
expect(isValid).toBe(true);
});
});

0 comments on commit 7d30d83

Please sign in to comment.