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
|
// TODO: conditional validation
|
||||||
// e.g. when algo is selected, Docker image is required
|
// 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
|
// hint, hint: https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema
|
||||||
|
const validationMetadata = {
|
||||||
const getValidationMetadata = async () => {
|
|
||||||
const { metadata } = await retrieveShaclSchema()
|
|
||||||
console.log(metadata)
|
|
||||||
|
|
||||||
return {
|
|
||||||
type: Yup.string()
|
type: Yup.string()
|
||||||
.matches(/dataset|algorithm/g, { excludeEmptyString: true })
|
.matches(/dataset|algorithm/g, { excludeEmptyString: true })
|
||||||
.required('Required'),
|
.required('Required'),
|
||||||
name: Yup.string()
|
name: Yup.string()
|
||||||
.min(4, (param) => `Title must be at least ${param.min} characters`)
|
.test(async function (value) {
|
||||||
.max(
|
// Use function
|
||||||
metadata.name.maxLength,
|
try {
|
||||||
(param) => `Description must have maximum ${param.max} characters`
|
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'),
|
.required('Required'),
|
||||||
description: Yup.string()
|
description: Yup.string()
|
||||||
.min(
|
.min(10, (param) => `Description must be at least ${param.min} characters`)
|
||||||
10,
|
|
||||||
(param) => `Description must be at least ${param.min} characters`
|
|
||||||
)
|
|
||||||
.max(
|
.max(
|
||||||
metadata.description.maxLength,
|
5000,
|
||||||
(param) => `Description must have maximum ${param.max} characters`
|
(param) => `Description must have maximum ${param.max} characters`
|
||||||
)
|
)
|
||||||
.required('Required'),
|
.required('Required'),
|
||||||
author: Yup.string()
|
author: Yup.string().required('Required'),
|
||||||
.max(
|
tags: Yup.string().nullable(),
|
||||||
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()
|
termsAndConditions: Yup.boolean()
|
||||||
.required('Required')
|
|
||||||
.isTrue('Please agree to the Terms and Conditions.')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const validationService = {
|
const validationService = {
|
||||||
@ -114,7 +109,7 @@ export const validationSchema: Yup.SchemaOf<any> = Yup.object().shape({
|
|||||||
chainId: Yup.number().required('Required'),
|
chainId: Yup.number().required('Required'),
|
||||||
accountId: Yup.string().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)),
|
services: Yup.array().of(Yup.object().shape(validationService)),
|
||||||
pricing: Yup.object().shape(validationPricing)
|
pricing: Yup.object().shape(validationPricing)
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user