Skip to content

Commit

Permalink
fix(token-list): increase token symbol validation length (#4913)
Browse files Browse the repository at this point in the history
* fix(token-list): increase token symbol validation length

* chore: fix regex
  • Loading branch information
shoom3301 authored Sep 25, 2024
1 parent 74b4391 commit 47b7115
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions libs/tokens/src/utils/validateTokenList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@ import type { TokenList } from '@uniswap/token-lists'

import type { Ajv, ValidateFunction } from 'ajv'

const SYMBOL_AND_NAME_VALIDATION = [
{
const: '',
},
{
pattern: '^[\\w\\d\\-\\+_\\.\\s]+$',
},
]

const patchValidationSchema = (schema: any) => ({
...schema,
definitions: {
...schema.definitions,
TokenInfo: {
...schema.definitions.TokenInfo,
properties: {
...schema.definitions.TokenInfo.properties,
symbol: {
...schema.definitions.TokenInfo.properties.symbol,
maxLength: 80,
anyOf: SYMBOL_AND_NAME_VALIDATION,
},
name: {
...schema.definitions.TokenInfo.properties.name,
maxLength: 100,
anyOf: SYMBOL_AND_NAME_VALIDATION,
},
},
},
},
})
enum ValidationSchema {
LIST = 'list',
TOKENS = 'tokens',
Expand All @@ -11,15 +42,15 @@ enum ValidationSchema {
const validator = new Promise<Ajv>((resolve) => {
Promise.all([import('ajv'), import('@uniswap/token-lists/src/tokenlist.schema.json')]).then(([ajv, schema]) => {
const validator = new ajv.default({ allErrors: true })
.addSchema(schema, ValidationSchema.LIST)
.addSchema(patchValidationSchema(schema), ValidationSchema.LIST)
// Adds a meta scheme of Pick<TokenList, 'tokens'>
.addSchema(
{
...schema,
...patchValidationSchema(schema),
$id: schema.$id + '#tokens',
required: ['tokens'],
},
ValidationSchema.TOKENS
ValidationSchema.TOKENS,
)
resolve(validator)
})
Expand Down

0 comments on commit 47b7115

Please sign in to comment.