From 65f0c5c675dc54adcba8dbfa65c089fae5039988 Mon Sep 17 00:00:00 2001 From: claudiaHash <49017601+claudiaHash@users.noreply.github.com> Date: Fri, 1 Apr 2022 11:38:32 +0300 Subject: [PATCH] Price length and decimals number validation (#1225) * price length and decimals number validation * undefined parameter fix * fixes * use the decimals constant in regex * validation added for moire fields * cleanup * swap fee decimals fixes * constant fix * use max 6 decimals for swap fee * remove unused code Co-authored-by: ClaudiaHolhos --- src/@utils/constants.ts | 2 +- src/components/Publish/Pricing/Price.tsx | 8 ++++++- src/components/Publish/_validation.ts | 30 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/@utils/constants.ts b/src/@utils/constants.ts index cf39b726d..ef3558975 100644 --- a/src/@utils/constants.ts +++ b/src/@utils/constants.ts @@ -1 +1 @@ -export const MAX_DECIMALS = 5 +export const MAX_DECIMALS = 6 diff --git a/src/components/Publish/Pricing/Price.tsx b/src/components/Publish/Pricing/Price.tsx index b18b80292..c55b92544 100644 --- a/src/components/Publish/Pricing/Price.tsx +++ b/src/components/Publish/Pricing/Price.tsx @@ -34,7 +34,13 @@ export default function Price({ <>
- +
diff --git a/src/components/Publish/_validation.ts b/src/components/Publish/_validation.ts index 7e4e50195..bbae3841f 100644 --- a/src/components/Publish/_validation.ts +++ b/src/components/Publish/_validation.ts @@ -1,8 +1,10 @@ +import { MAX_DECIMALS } from '@utils/constants' import * as Yup from 'yup' // TODO: conditional validation // e.g. when algo is selected, Docker image is required // hint, hint: https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema + const validationMetadata = { type: Yup.string() .matches(/dataset|algorithm/g, { excludeEmptyString: true }) @@ -54,25 +56,53 @@ const validationService = { }) } +const maxDecimalsValidation = new RegExp( + '^\\d+(\\.\\d{1,' + MAX_DECIMALS + '})?$' +) + const validationPricing = { type: Yup.string() .matches(/fixed|dynamic|free/g, { excludeEmptyString: true }) .required('Required'), // https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema + price: Yup.number() .min(1, (param: { min: number }) => `Must be more or equal to ${param.min}`) + .max( + 1000000, + (param: { max: number }) => `Must be less than or equal to ${param.max}` + ) + .test( + 'maxDigitsAfterDecimal', + `Must have maximum ${MAX_DECIMALS} decimal digits`, + (param) => maxDecimalsValidation.test(param?.toString()) + ) .required('Required'), amountDataToken: Yup.number() .min(50, (param) => `Must be more or equal to ${param.min}`) .required('Required'), amountOcean: Yup.number() .min(50, (param) => `Must be more or equal to ${param.min}`) + .max( + 1000000, + (param: { max: number }) => `Must be less than or equal to ${param.max}` + ) + .test( + 'maxDigitsAfterDecimal', + `Must have maximum ${MAX_DECIMALS} decimal digits`, + (param) => maxDecimalsValidation.test(param?.toString()) + ) .required('Required'), weightOnDataToken: Yup.string().required('Required'), weightOnOcean: Yup.string().required('Required'), swapFee: Yup.number() .min(0.1, (param) => `Must be more or equal to ${param.min}`) .max(10, 'Maximum is 10%') + .test( + 'maxDigitsAfterDecimal', + `Must have maximum ${MAX_DECIMALS} decimal digits`, + (param) => maxDecimalsValidation.test(param?.toString()) + ) .required('Required') }