mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
dynamic validation on name field
WIP: looking at another solution to dynamically validate all the fields instead of one by one
This commit is contained in:
parent
dc54ba3b93
commit
1c2dacfa25
@ -6,48 +6,43 @@ import { retrieveShaclSchema } from '@utils/aquarius'
|
||||
// TODO: conditional validation
|
||||
// e.g. when algo is selected, Docker image is required
|
||||
// hint, hint: https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema
|
||||
|
||||
const getValidationMetadata = async () => {
|
||||
const { metadata } = await retrieveShaclSchema()
|
||||
console.log(metadata)
|
||||
|
||||
return {
|
||||
type: Yup.string()
|
||||
.matches(/dataset|algorithm/g, { excludeEmptyString: true })
|
||||
.required('Required'),
|
||||
name: Yup.string()
|
||||
.min(4, (param) => `Title must be at least ${param.min} characters`)
|
||||
.max(
|
||||
metadata.name.maxLength,
|
||||
(param) => `Description must have maximum ${param.max} characters`
|
||||
)
|
||||
.required('Required'),
|
||||
description: Yup.string()
|
||||
.min(
|
||||
10,
|
||||
(param) => `Description must be at least ${param.min} characters`
|
||||
)
|
||||
.max(
|
||||
metadata.description.maxLength,
|
||||
(param) => `Description must have maximum ${param.max} characters`
|
||||
)
|
||||
.required('Required'),
|
||||
author: Yup.string()
|
||||
.max(
|
||||
metadata.author.maxLength,
|
||||
(param) => `Author must have maximum ${param.max} characters`
|
||||
)
|
||||
.required('Required'),
|
||||
tags: Yup.string()
|
||||
.max(
|
||||
metadata.tags.maxLength,
|
||||
(param) => `Tags must have maximum ${param.max} characters`
|
||||
)
|
||||
.nullable(),
|
||||
termsAndConditions: Yup.boolean()
|
||||
.required('Required')
|
||||
.isTrue('Please agree to the Terms and Conditions.')
|
||||
}
|
||||
const validationMetadata = {
|
||||
type: Yup.string()
|
||||
.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'),
|
||||
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(),
|
||||
termsAndConditions: Yup.boolean()
|
||||
}
|
||||
|
||||
const validationService = {
|
||||
@ -114,7 +109,7 @@ export const validationSchema: Yup.SchemaOf<any> = Yup.object().shape({
|
||||
chainId: Yup.number().required('Required'),
|
||||
accountId: Yup.string().required('Required')
|
||||
}),
|
||||
metadata: Yup.object().shape(getValidationMetadata()),
|
||||
metadata: Yup.object().shape(validationMetadata),
|
||||
services: Yup.array().of(Yup.object().shape(validationService)),
|
||||
pricing: Yup.object().shape(validationPricing)
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user