-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: verification endpoint alignment with etherscan
- Loading branch information
Showing
5 changed files
with
115 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/api/src/common/decorators/formatAndValidateCompilerVersion.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { validate } from "class-validator"; | ||
import { FormatAndValidateCompilerVersion } from "./formatAndValidateCompilerVersion"; | ||
|
||
class TestDto { | ||
constructor(version: string) { | ||
this.version = version; | ||
} | ||
|
||
@FormatAndValidateCompilerVersion() | ||
public version: string; | ||
} | ||
|
||
describe("FormatAndValidateCompilerVersion", () => { | ||
it("when version is null returns a validation error", async () => { | ||
const errors = await validate(new TestDto(null)); | ||
expect(errors.length).toBe(1); | ||
expect(errors[0].property).toBe("version"); | ||
}); | ||
|
||
it("when version is an empty string returns a validation error", async () => { | ||
const errors = await validate(new TestDto("")); | ||
expect(errors.length).toBe(1); | ||
expect(errors[0].property).toBe("version"); | ||
}); | ||
|
||
it("when version is a valid", async () => { | ||
const errors = await validate(new TestDto("2.3.7")); | ||
expect(errors.length).toBe(0); | ||
}); | ||
|
||
it("when version is valid with commit", async () => { | ||
const errors = await validate(new TestDto("2.5.7-commit.32")); | ||
expect(errors.length).toBe(0); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/api/src/common/decorators/formatAndValidateCompilerVersion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { registerDecorator, ValidationOptions } from "class-validator"; | ||
export function FormatAndValidateCompilerVersion(validationOptions?: ValidationOptions) { | ||
return function (object: any, propertyName: string) { | ||
registerDecorator({ | ||
name: "formatAndValidateCompilerVersion", | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
options: validationOptions, | ||
validator: { | ||
validate(value: any) { | ||
return value && typeof value === "string"; | ||
}, | ||
}, | ||
}); | ||
// Custom setter to format the value | ||
Object.defineProperty(object, propertyName, { | ||
set(value: string) { | ||
const regex = /^(0\.\d+\.\d+(\.\d+)?|zkVM-\d+\.\d+\.\d+(\.\d+)?-\d+\.\d+\.\d+(\.\d+)?)$/; | ||
if (value && !regex.test(value)) { | ||
let [major, minor, patch] = value.split("."); | ||
major = major.slice(1); | ||
patch = patch.replace(/\+.*$/, ""); | ||
minor = minor; | ||
const formattedValue = `${major}.${minor}.${patch}`; | ||
Object.defineProperty(object, `_${propertyName}`, { | ||
value: formattedValue, | ||
writable: true, | ||
configurable: true, | ||
}); | ||
} else { | ||
Object.defineProperty(object, `_${propertyName}`, { | ||
value: value, | ||
writable: true, | ||
configurable: true, | ||
}); | ||
} | ||
}, | ||
get() { | ||
return this[`_${propertyName}`]; | ||
}, | ||
}); | ||
}; | ||
} |