-
Notifications
You must be signed in to change notification settings - Fork 41
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
Migrate Token to zod #1317
Conversation
Pull Request Test Coverage Report for Build 8327871145Details
💛 - Coveralls |
export const TokenSchema = z.object({ | ||
address: AddressSchema, | ||
decimals: z.number().nullish().default(null), | ||
logoUri: z.string(), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🙂
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary
This migrates the validation of
Token
tozod
.Changes
TokenValidator
and associated schemas.TokenSchema
and infer type from it.