1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

added dynamic validation to metadata in publish

This commit is contained in:
EnzoVezzaro 2022-09-22 10:24:03 -04:00
parent d368c08b85
commit 52a606d0e2

View File

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