From 74a9d074edb73ac470e32b7577cffe51862751d4 Mon Sep 17 00:00:00 2001 From: EnzoVezzaro Date: Mon, 26 Sep 2022 09:01:51 -0400 Subject: [PATCH] fix file validation on edit form (#1677) * split validation / constants * some minor refactor * fix edit form (duplication ErrorMessage) * fix Feedback UI * remove logs * fix empty space when loading total sales * added isTrue to valid * remove hardcoded FileInfo typing in publish and edit * fix more FileInfo typing * fix missing error message on inputs --- src/components/@shared/FormInput/index.tsx | 8 ++--- src/components/@shared/Price/Conversion.tsx | 2 +- .../Asset/Edit/EditComputeDataset.tsx | 6 ++-- .../Asset/Edit/EditFeedback.module.css | 1 + src/components/Asset/Edit/EditMetadata.tsx | 3 +- src/components/Asset/Edit/_constants.ts | 25 ++------------ src/components/Asset/Edit/_types.ts | 5 +-- src/components/Asset/Edit/_validation.ts | 34 +++++++++++++++++++ src/components/Publish/_constants.tsx | 4 +-- src/components/Publish/_types.ts | 10 +----- src/components/Publish/_validation.ts | 5 +-- 11 files changed, 56 insertions(+), 47 deletions(-) create mode 100644 src/components/Asset/Edit/_validation.ts diff --git a/src/components/@shared/FormInput/index.tsx b/src/components/@shared/FormInput/index.tsx index 9f5b05bef..9079fed45 100644 --- a/src/components/@shared/FormInput/index.tsx +++ b/src/components/@shared/FormInput/index.tsx @@ -71,9 +71,7 @@ function checkError( parsedFieldName: string[], field: FieldInputProps ) { - if (form?.errors && Object.entries(form?.errors).length === 0) { - return false - } else if ( + if ( (form?.touched?.[parsedFieldName[0]]?.[parsedFieldName[1]] && form?.errors?.[parsedFieldName[0]]?.[parsedFieldName[1]]) || (form?.touched[field.name] && @@ -140,11 +138,13 @@ export default function Input(props: Partial): ReactElement { {help && prominentHelp && {help}} - {isFormikField && hasFormikError && ( + + {field?.name !== 'files' && isFormikField && hasFormikError && (
)} + {disclaimer && ( {disclaimer} )} diff --git a/src/components/@shared/Price/Conversion.tsx b/src/components/@shared/Price/Conversion.tsx index 625b148df..509395ce8 100644 --- a/src/components/@shared/Price/Conversion.tsx +++ b/src/components/@shared/Price/Conversion.tsx @@ -52,7 +52,7 @@ export default function Conversion({ setPriceConverted(convertedFormattedHTMLstring) }, [price, prices, currency, locale, isFiat, priceTokenId]) - return price > 0 ? ( + return Number(price) >= 0 ? ( `Title must be at least ${param.min} characters`) - .required('Required'), - description: Yup.string().required('Required').min(10), - price: Yup.number().required('Required'), - links: Yup.array().nullable(), - files: Yup.array().nullable(), - timeout: Yup.string().required('Required'), - author: Yup.string().nullable() -}) - export function getInitialValues( metadata: Metadata, timeout: number, @@ -24,19 +11,13 @@ export function getInitialValues( name: metadata?.name, description: metadata?.description, price, - links: metadata?.links, - files: '', + links: metadata?.links as any, + files: [{ url: '', type: '' }], timeout: secondsToString(timeout), author: metadata?.author } } -export const computeSettingsValidationSchema = Yup.object().shape({ - allowAllPublishedAlgorithms: Yup.boolean().nullable(), - publisherTrustedAlgorithms: Yup.array().nullable(), - publisherTrustedAlgorithmPublishers: Yup.array().nullable() -}) - export function getComputeSettingsInitialValues({ publisherTrustedAlgorithms, publisherTrustedAlgorithmPublishers diff --git a/src/components/Asset/Edit/_types.ts b/src/components/Asset/Edit/_types.ts index 6587bc75b..c36b968b6 100644 --- a/src/components/Asset/Edit/_types.ts +++ b/src/components/Asset/Edit/_types.ts @@ -1,10 +1,11 @@ +import { FileInfo } from '@oceanprotocol/lib' export interface MetadataEditForm { name: string description: string timeout: string price?: string - links?: string | any[] - files: string | any[] + files: FileInfo[] + links?: FileInfo[] author?: string } diff --git a/src/components/Asset/Edit/_validation.ts b/src/components/Asset/Edit/_validation.ts new file mode 100644 index 000000000..9b40e5507 --- /dev/null +++ b/src/components/Asset/Edit/_validation.ts @@ -0,0 +1,34 @@ +import { FileInfo } from '@oceanprotocol/lib' +import * as Yup from 'yup' + +export const validationSchema = Yup.object().shape({ + name: Yup.string() + .min(4, (param) => `Title must be at least ${param.min} characters`) + .required('Required'), + description: Yup.string().required('Required').min(10), + price: Yup.number().required('Required'), + files: Yup.array() + .of( + Yup.object().shape({ + url: Yup.string().url('Must be a valid URL.'), + valid: Yup.boolean().isTrue() + }) + ) + .nullable(), + links: Yup.array() + .of( + Yup.object().shape({ + url: Yup.string().url('Must be a valid URL.'), + valid: Yup.boolean().isTrue() + }) + ) + .nullable(), + timeout: Yup.string().required('Required'), + author: Yup.string().nullable() +}) + +export const computeSettingsValidationSchema = Yup.object().shape({ + allowAllPublishedAlgorithms: Yup.boolean().nullable(), + publisherTrustedAlgorithms: Yup.array().nullable(), + publisherTrustedAlgorithmPublishers: Yup.array().nullable() +}) diff --git a/src/components/Publish/_constants.tsx b/src/components/Publish/_constants.tsx index 19eba3f69..38d9b9119 100644 --- a/src/components/Publish/_constants.tsx +++ b/src/components/Publish/_constants.tsx @@ -72,8 +72,8 @@ export const initialValues: FormPublishData = { }, services: [ { - files: [{ url: '' }], - links: [{ url: '' }], + files: [{ url: '', type: '' }], + links: [{ url: '', type: '' }], dataTokenOptions: { name: '', symbol: '' }, timeout: '', access: 'access', diff --git a/src/components/Publish/_types.ts b/src/components/Publish/_types.ts index bae8306a3..d313c2dde 100644 --- a/src/components/Publish/_types.ts +++ b/src/components/Publish/_types.ts @@ -1,14 +1,6 @@ -import { ServiceComputeOptions } from '@oceanprotocol/lib' +import { FileInfo, ServiceComputeOptions } from '@oceanprotocol/lib' import { NftMetadata } from '@utils/nft' import { ReactElement } from 'react' - -interface FileInfo { - url: string - valid?: boolean - contentLength?: string - contentType?: string -} - export interface FormPublishService { files: FileInfo[] links?: FileInfo[] diff --git a/src/components/Publish/_validation.ts b/src/components/Publish/_validation.ts index 39ac4245a..e15ae9594 100644 --- a/src/components/Publish/_validation.ts +++ b/src/components/Publish/_validation.ts @@ -1,6 +1,7 @@ import { MAX_DECIMALS } from '@utils/constants' import * as Yup from 'yup' import { getMaxDecimalsValidation } from '@utils/numbers' +import { FileInfo } from '@oceanprotocol/lib' // TODO: conditional validation // e.g. when algo is selected, Docker image is required @@ -28,7 +29,7 @@ const validationMetadata = { } const validationService = { - files: Yup.array<{ url: string; valid: boolean }[]>() + files: Yup.array() .of( Yup.object().shape({ url: Yup.string().url('Must be a valid URL.').required('Required'), @@ -37,7 +38,7 @@ const validationService = { ) .min(1, `At least one file is required.`) .required('Enter a valid URL and click ADD FILE.'), - links: Yup.array<{ url: string; valid: boolean }[]>() + links: Yup.array() .of( Yup.object().shape({ url: Yup.string().url('Must be a valid URL.'),