2022-04-01 10:38:32 +02:00
|
|
|
import { MAX_DECIMALS } from '@utils/constants'
|
2021-11-23 18:00:24 +01:00
|
|
|
import * as Yup from 'yup'
|
2022-04-22 02:38:35 +02:00
|
|
|
import { getMaxDecimalsValidation } from '@utils/numbers'
|
2022-09-26 15:01:51 +02:00
|
|
|
import { FileInfo } from '@oceanprotocol/lib'
|
2021-11-23 18:00:24 +01:00
|
|
|
|
|
|
|
// 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
|
2022-04-01 10:38:32 +02:00
|
|
|
|
2021-11-23 18:00:24 +01:00
|
|
|
const validationMetadata = {
|
|
|
|
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`)
|
|
|
|
.required('Required'),
|
|
|
|
description: Yup.string()
|
|
|
|
.min(10, (param) => `Description must be at least ${param.min} characters`)
|
2022-04-15 14:33:51 +02:00
|
|
|
.max(
|
|
|
|
5000,
|
|
|
|
(param) => `Description must have maximum ${param.max} characters`
|
|
|
|
)
|
2021-11-23 18:00:24 +01:00
|
|
|
.required('Required'),
|
|
|
|
author: Yup.string().required('Required'),
|
2022-10-05 16:40:00 +02:00
|
|
|
tags: Yup.array<string[]>().nullable(),
|
2021-11-25 15:36:01 +01:00
|
|
|
termsAndConditions: Yup.boolean()
|
|
|
|
.required('Required')
|
|
|
|
.isTrue('Please agree to the Terms and Conditions.')
|
2021-11-23 18:00:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const validationService = {
|
2022-09-26 15:01:51 +02:00
|
|
|
files: Yup.array<FileInfo[]>()
|
2021-11-23 18:00:24 +01:00
|
|
|
.of(
|
|
|
|
Yup.object().shape({
|
2022-11-01 16:00:07 +01:00
|
|
|
url: Yup.string()
|
|
|
|
.test(
|
|
|
|
'GoogleNotSupported',
|
|
|
|
'Google Drive is not a supported hosting service. Please use an alternative.',
|
|
|
|
(value) => {
|
|
|
|
return !value?.toString().includes('drive.google')
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.url('Must be a valid URL.')
|
|
|
|
.required('Required'),
|
|
|
|
|
2021-11-23 18:00:24 +01:00
|
|
|
valid: Yup.boolean().isTrue().required('File must be valid.')
|
|
|
|
})
|
|
|
|
)
|
2022-08-02 11:53:22 +02:00
|
|
|
.min(1, `At least one file is required.`)
|
2021-11-23 18:00:24 +01:00
|
|
|
.required('Enter a valid URL and click ADD FILE.'),
|
2022-09-26 15:01:51 +02:00
|
|
|
links: Yup.array<FileInfo[]>()
|
2021-11-23 18:00:24 +01:00
|
|
|
.of(
|
|
|
|
Yup.object().shape({
|
2022-11-01 16:00:07 +01:00
|
|
|
url: Yup.string()
|
|
|
|
.url('Must be a valid URL.')
|
|
|
|
.test(
|
|
|
|
'GoogleNotSupported',
|
|
|
|
'Google Drive is not a supported hosting service. Please use an alternative.',
|
|
|
|
(value) => {
|
|
|
|
return !value?.toString().includes('drive.google')
|
|
|
|
}
|
|
|
|
),
|
2021-11-24 16:46:13 +01:00
|
|
|
// TODO: require valid file only when URL is given
|
|
|
|
valid: Yup.boolean()
|
|
|
|
// valid: Yup.boolean().isTrue('File must be valid.')
|
2021-11-23 18:00:24 +01:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.nullable(),
|
|
|
|
dataTokenOptions: Yup.object().shape({
|
|
|
|
name: Yup.string(),
|
|
|
|
symbol: Yup.string()
|
|
|
|
}),
|
|
|
|
timeout: Yup.string().required('Required'),
|
|
|
|
access: Yup.string()
|
2021-12-21 21:26:08 +01:00
|
|
|
.matches(/compute|access/g)
|
2021-11-23 18:00:24 +01:00
|
|
|
.required('Required'),
|
2021-11-25 15:16:42 +01:00
|
|
|
providerUrl: Yup.object().shape({
|
|
|
|
url: Yup.string().url('Must be a valid URL.').required('Required'),
|
2022-04-15 15:28:45 +02:00
|
|
|
valid: Yup.boolean().isTrue().required('Valid Provider is required.'),
|
|
|
|
custom: Yup.boolean()
|
2021-11-25 15:16:42 +01:00
|
|
|
})
|
2021-11-23 18:00:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const validationPricing = {
|
|
|
|
type: Yup.string()
|
2022-08-02 11:53:22 +02:00
|
|
|
.matches(/fixed|free/g, { excludeEmptyString: true })
|
2021-11-23 18:00:24 +01:00
|
|
|
.required('Required'),
|
|
|
|
// https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema
|
2022-04-01 10:38:32 +02:00
|
|
|
|
2021-11-23 18:00:24 +01:00
|
|
|
price: Yup.number()
|
2021-11-26 14:05:53 +01:00
|
|
|
.min(1, (param: { min: number }) => `Must be more or equal to ${param.min}`)
|
2022-04-01 10:38:32 +02:00
|
|
|
.max(
|
|
|
|
1000000,
|
|
|
|
(param: { max: number }) => `Must be less than or equal to ${param.max}`
|
|
|
|
)
|
|
|
|
.test(
|
|
|
|
'maxDigitsAfterDecimal',
|
|
|
|
`Must have maximum ${MAX_DECIMALS} decimal digits`,
|
2022-04-22 02:38:35 +02:00
|
|
|
(param) => getMaxDecimalsValidation(MAX_DECIMALS).test(param?.toString())
|
2022-04-01 10:38:32 +02:00
|
|
|
)
|
2021-11-23 18:00:24 +01:00
|
|
|
.required('Required')
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make Yup.SchemaOf<FormPublishData> work, requires conditional validation
|
|
|
|
// of all the custom docker image stuff.
|
|
|
|
// export const validationSchema: Yup.SchemaOf<FormPublishData> =
|
|
|
|
export const validationSchema: Yup.SchemaOf<any> = Yup.object().shape({
|
|
|
|
user: Yup.object().shape({
|
|
|
|
stepCurrent: Yup.number(),
|
|
|
|
chainId: Yup.number().required('Required'),
|
|
|
|
accountId: Yup.string().required('Required')
|
|
|
|
}),
|
|
|
|
metadata: Yup.object().shape(validationMetadata),
|
|
|
|
services: Yup.array().of(Yup.object().shape(validationService)),
|
|
|
|
pricing: Yup.object().shape(validationPricing)
|
|
|
|
})
|