Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Token to zod #1317

Merged
merged 3 commits into from
Mar 18, 2024
Merged

Migrate Token to zod #1317

merged 3 commits into from
Mar 18, 2024

Conversation

hectorgomezv
Copy link
Member

Summary

This migrates the validation of Token to zod.

Changes

  • Remove TokenValidator and associated schemas.
  • Add test-covered TokenSchema and infer type from it.
  • Propagate type requirements.
  • Update tests accordingly.

@hectorgomezv hectorgomezv requested a review from a team as a code owner March 18, 2024 13:20
@hectorgomezv hectorgomezv self-assigned this Mar 18, 2024
@hectorgomezv hectorgomezv changed the title Migrate Singleton to zod Migrate Token to zod Mar 18, 2024
@coveralls
Copy link

coveralls commented Mar 18, 2024

Pull Request Test Coverage Report for Build 8327871145

Details

  • 10 of 11 (90.91%) changed or added relevant lines in 4 files are covered.
  • 2 unchanged lines in 2 files lost coverage.
  • Overall coverage remained the same at 93.671%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/domain/tokens/token.repository.ts 2 3 66.67%
Files with Coverage Reduction New Missed Lines %
src/domain/safe/safe.repository.ts 1 99.13%
src/routes/transactions/entities/tests/human-description.builder.ts 1 80.0%
Totals Coverage Status
Change from base Build 8327037169: 0.0%
Covered Lines: 6513
Relevant Lines: 6720

💛 - Coveralls

export const TokenSchema = z.object({
address: AddressSchema,
decimals: z.number().nullish().default(null),
logoUri: z.string(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would've said that this could be a z.string().url() but I see it's just a string in AJV. Perhaps we should revisit this later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this maybe this would be nullable as well according to the Transaction Service docs, but we can revisit it later as it might need adjustments in other parts of the code as well 🙂

Comment on lines +6 to +16
export const TokenSchema = z.object({
address: AddressSchema,
decimals: z.number().nullish().default(null),
logoUri: z.string(),
name: z.string(),
symbol: z.string(),
type: z.nativeEnum(TokenType),
trusted: z.boolean(),
});

export const tokenSchema: JSONSchemaType<Token> = {
$id: TOKEN_SCHEMA_ID,
type: 'object',
properties: {
address: { type: 'string' },
decimals: { oneOf: [{ type: 'number' }, { type: 'null', nullable: true }] },
logoUri: { type: 'string' },
name: { type: 'string' },
symbol: { type: 'string' },
type: { type: 'string', enum: Object.values(TokenType) },
trusted: { type: 'boolean' },
},
required: [
'address',
'decimals',
'logoUri',
'name',
'symbol',
'type',
'trusted',
],
};

export const TOKEN_PAGE_SCHEMA_ID =
'https://safe-client.safe.global/schemas/tokens/token-page.json';

export const tokenPageSchema: Schema = buildPageSchema(
TOKEN_PAGE_SCHEMA_ID,
tokenSchema,
);
export const TokenPageSchema = buildZodPageSchema(TokenSchema);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add tests for these?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 316e401

And adapted to the new constraints in cccaf2e

@hectorgomezv hectorgomezv merged commit a372b08 into main Mar 18, 2024
16 checks passed
@hectorgomezv hectorgomezv deleted the zod-token branch March 18, 2024 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants