diff --git a/src/components/Publish/_validation.ts b/src/components/Publish/_validation.ts index 732a9fa82..624044187 100644 --- a/src/components/Publish/_validation.ts +++ b/src/components/Publish/_validation.ts @@ -2,6 +2,11 @@ import { MAX_DECIMALS } from '@utils/constants' import * as Yup from 'yup' import { getMaxDecimalsValidation } from '@utils/numbers' import { retrieveShaclSchema } from '@utils/aquarius' +import { + ShaclSchema, + ShaclSchemaField +} from '@context/MarketMetadata/_shaclType' +import { capitalizeFirstLetter } from '@utils/textTransform' // TODO: conditional validation // e.g. when algo is selected, Docker image is required @@ -11,37 +16,101 @@ const validationMetadata = { .matches(/dataset|algorithm/g, { excludeEmptyString: true }) .required('Required'), name: Yup.string() - .test(async function (value) { - // Use function - try { - const schema = await retrieveShaclSchema() - const fieldValidation = schema.metadata.name - if (value.length < 4) { - // TODO: replace hardcoded with resp from schacl when integrated - return this.createError({ - message: `Name must be at least ${4} characters` - }) - } else if (value.length > fieldValidation.maxLength) { - return this.createError({ - message: `Name must have maximum ${fieldValidation.maxLength} characters` - }) - } else { - return value - } - } catch (e) { - console.log(e) + .required('Required') + .test(async (value, { path, createError }): Promise => { + if (!value) return + const schemaField: any = await retrieveShaclSchema() + const fieldValidation: ShaclSchemaField = + schemaField[path.split('.')[0]][path.split('.')[1]] + // TODO: add minLength when integrated in endpoint + if (value.length < 10) { + return createError({ + message: `${capitalizeFirstLetter( + path.split('.')[1] + )} must be at least ${10} characters` + }) + } else if (value.length > fieldValidation.maxLength) { + return createError({ + message: `${capitalizeFirstLetter( + path.split('.')[1] + )} must have maximum ${fieldValidation.maxLength} characters` + }) + } else { + return value } - }) - .required('Required'), + }), description: Yup.string() - .min(10, (param) => `Description must be at least ${param.min} characters`) - .max( - 5000, - (param) => `Description must have maximum ${param.max} characters` - ) - .required('Required'), - author: Yup.string().required('Required'), - tags: Yup.string().nullable(), + .required('Required') + .test(async (value, { path, createError }): Promise => { + if (!value) return + const schemaField: any = await retrieveShaclSchema() + const fieldValidation: ShaclSchemaField = + schemaField[path.split('.')[0]][path.split('.')[1]] + // TODO: add minLength when integrated in endpoint + if (value.length < 10) { + return createError({ + message: `${capitalizeFirstLetter( + path.split('.')[1] + )} must be at least ${10} characters` + }) + } else if (value.length > fieldValidation.maxLength) { + return createError({ + message: `${capitalizeFirstLetter( + path.split('.')[1] + )} must have maximum ${fieldValidation.maxLength} characters` + }) + } else { + return value + } + }), + author: Yup.string() + .required('Required') + .test(async (value, { path, createError }): Promise => { + if (!value) return + const schemaField: any = await retrieveShaclSchema() + const fieldValidation: ShaclSchemaField = + schemaField[path.split('.')[0]][path.split('.')[1]] + // TODO: add minLength when integrated in endpoint + if (value.length < 1) { + return createError({ + message: `${capitalizeFirstLetter( + path.split('.')[1] + )} must be at least ${1} characters` + }) + } else if (value.length > fieldValidation.maxLength) { + return createError({ + message: `${capitalizeFirstLetter( + path.split('.')[1] + )} must have maximum ${fieldValidation.maxLength} characters` + }) + } else { + return value + } + }), + tags: Yup.string() + .nullable(true) + .test(async (value, { path, createError }): Promise => { + if (!value) return true + const schemaField: any = await retrieveShaclSchema() + const fieldValidation: ShaclSchemaField = + schemaField[path.split('.')[0]][path.split('.')[1]] + // TODO: add minLength when integrated in endpoint + if (value.length < 1) { + return createError({ + message: `${capitalizeFirstLetter( + path.split('.')[1] + )} must be at least ${1} characters` + }) + } else if (value.length > fieldValidation.maxLength) { + return createError({ + message: `${capitalizeFirstLetter( + path.split('.')[1] + )} must have maximum ${fieldValidation.maxLength} characters` + }) + } else { + return value + } + }), termsAndConditions: Yup.boolean() }