Skip to content

Commit

Permalink
prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
fullkomnun committed Sep 21, 2023
1 parent 913fdd9 commit 5b77579
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
6 changes: 3 additions & 3 deletions packages/web3-validator/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ const convertToZod = (schema: JsonSchema): ZodType => {
}

if (schema?.format) {
if (!formats[schema.format]) {
throw new Error(`format ${schema.format} is unsupported`);
}
if (!formats[schema.format]) {
throw new Error(`format ${schema.format} is unsupported`);
}

return z.any().refine(formats[schema.format], (value: unknown) => ({
params: { value, format: schema.format },
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-validator/test/config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
},
verbose: false,
collectCoverage: false,
coverageReporters: ['json'],
Expand Down
54 changes: 26 additions & 28 deletions packages/web3-validator/test/fixtures/abi_to_json_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1661,36 +1661,34 @@ const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [
];

function generateSingleParamNumericCase(type: string, bitSize: number) {
return {
title: `single param ${type}${bitSize}`,
abi: {
fullSchema: [{ name: 'a', type: `${type}${bitSize}` }],
shortSchema: [`${type}${bitSize}`],
data: [12],
},
json: {
fullSchema: {
type: 'array',
items: [{ $id: 'a', format: `${type}${bitSize}`, required: true }],
minItems: 1,
maxItems: 1,
},
shortSchema: {
type: 'array',
items: [{ $id: '/0/0', format: `${type}${bitSize}`, required: true }],
minItems: 1,
maxItems: 1,
},
data: [12],
},
};
return {
title: `single param ${type}${bitSize}`,
abi: {
fullSchema: [{ name: 'a', type: `${type}${bitSize}` }],
shortSchema: [`${type}${bitSize}`],
data: [12],
},
json: {
fullSchema: {
type: 'array',
items: [{ $id: 'a', format: `${type}${bitSize}`, required: true }],
minItems: 1,
maxItems: 1,
},
shortSchema: {
type: 'array',
items: [{ $id: '/0/0', format: `${type}${bitSize}`, required: true }],
minItems: 1,
maxItems: 1,
},
data: [12],
},
};
}

for (let i = 256; i >= 8; i -= 8) {
abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('int', i));
abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('uint', i));
abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('int', i));
abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('uint', i));
}

export {
abiToJsonSchemaCases
};
export { abiToJsonSchemaCases };
17 changes: 10 additions & 7 deletions packages/web3-validator/test/unit/web3_validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,16 @@ describe('web3-validator', () => {
});
});
describe('validateJsonSchema', () => {
// only single param test cases
it.each(abiToJsonSchemaCases.slice(0, 37))(`$title - should pass for valid data`, abi => {
const jsonSchema = abi.json;
expect(
validator.validateJSONSchema(jsonSchema.fullSchema, jsonSchema.data),
).toBeUndefined();
});
// only single param test cases
it.each(abiToJsonSchemaCases.slice(0, 37))(
`$title - should pass for valid data`,
abi => {
const jsonSchema = abi.json;
expect(
validator.validateJSONSchema(jsonSchema.fullSchema, jsonSchema.data),
).toBeUndefined();
},
);

it('should throw', () => {
expect(() => {
Expand Down

0 comments on commit 5b77579

Please sign in to comment.