From 47b7115f75487667f4f850dfb5469e6bbe22aba5 Mon Sep 17 00:00:00 2001 From: Alexandr Kazachenko Date: Wed, 25 Sep 2024 10:53:33 +0500 Subject: [PATCH] fix(token-list): increase token symbol validation length (#4913) * fix(token-list): increase token symbol validation length * chore: fix regex --- libs/tokens/src/utils/validateTokenList.ts | 37 ++++++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/libs/tokens/src/utils/validateTokenList.ts b/libs/tokens/src/utils/validateTokenList.ts index b0c1ad2226..e841ef950d 100644 --- a/libs/tokens/src/utils/validateTokenList.ts +++ b/libs/tokens/src/utils/validateTokenList.ts @@ -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', @@ -11,15 +42,15 @@ enum ValidationSchema { const validator = new Promise((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 .addSchema( { - ...schema, + ...patchValidationSchema(schema), $id: schema.$id + '#tokens', required: ['tokens'], }, - ValidationSchema.TOKENS + ValidationSchema.TOKENS, ) resolve(validator) })