From ba245e2698da8f0f694fa15daf3aaf51064438db Mon Sep 17 00:00:00 2001 From: EnzoVezzaro Date: Mon, 26 Sep 2022 08:09:57 -0400 Subject: [PATCH] refactor shacl schema validation --- src/@utils/schaclSchema.ts | 60 +++++++++++++ src/components/Publish/_validation.ts | 121 ++++++++------------------ 2 files changed, 98 insertions(+), 83 deletions(-) create mode 100644 src/@utils/schaclSchema.ts diff --git a/src/@utils/schaclSchema.ts b/src/@utils/schaclSchema.ts new file mode 100644 index 000000000..3d73df8d2 --- /dev/null +++ b/src/@utils/schaclSchema.ts @@ -0,0 +1,60 @@ +import { retrieveShaclSchema } from '@utils/aquarius' +import { ShaclSchemaField } from '@context/MarketMetadata/_shaclType' + +import { capitalizeFirstLetter } from '@utils/textTransform' + +function getMinMax(valueField: string, field: ShaclSchemaField) { + let min = field.minLength + const max = field.maxLength + + // TODO: remove once all fields in schema has minLength property + if (!min) { + switch (valueField) { + case 'name': + min = 4 + break + case 'description': + min = 10 + break + case 'author': + min = 1 + break + case 'tags': + min = 1 + break + } + } + + return { + minLength: min, + maxLength: max + } +} + +export async function validateFieldSchaclSchema( + keyField: string, + valueField: string, + value: any, + createError: any +): Promise { + const schemaField: any = await retrieveShaclSchema() + const fieldSchema: ShaclSchemaField = schemaField[keyField][valueField] + const { minLength, maxLength } = getMinMax(valueField, fieldSchema) + + // TODO: add minLength when integrated in endpoint + if (value.length < minLength) { + return createError({ + message: `${capitalizeFirstLetter( + valueField + )} must be at least ${minLength} characters` + }) + } else if (value.length > maxLength) { + return createError({ + message: `${capitalizeFirstLetter( + valueField + )} must have maximum ${maxLength} characters` + }) + } else { + return value + } +} diff --git a/src/components/Publish/_validation.ts b/src/components/Publish/_validation.ts index 624044187..7fcde2c56 100644 --- a/src/components/Publish/_validation.ts +++ b/src/components/Publish/_validation.ts @@ -1,12 +1,7 @@ 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' +import { validateFieldSchaclSchema } from '@utils/schaclSchema' // TODO: conditional validation // e.g. when algo is selected, Docker image is required @@ -19,97 +14,57 @@ const validationMetadata = { .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 - } + const keyField = path.split('.')[0] + const valueField = path.split('.')[1] + + return await validateFieldSchaclSchema( + keyField, + valueField, + value, + createError + ) }), description: 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 < 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 - } + const keyField = path.split('.')[0] + const valueField = path.split('.')[1] + + return await validateFieldSchaclSchema( + keyField, + valueField, + value, + createError + ) }), 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 - } + const keyField = path.split('.')[0] + const valueField = path.split('.')[1] + + return await validateFieldSchaclSchema( + keyField, + valueField, + value, + createError + ) }), 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 - } + if (!value) return + const keyField = path.split('.')[0] + const valueField = path.split('.')[1] + + return await validateFieldSchaclSchema( + keyField, + valueField, + value, + createError + ) }), termsAndConditions: Yup.boolean() }