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 { 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<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 < 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<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 < 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()
}