mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
refactor shacl schema validation
This commit is contained in:
parent
d4d6f53256
commit
ba245e2698
60
src/@utils/schaclSchema.ts
Normal file
60
src/@utils/schaclSchema.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import { retrieveShaclSchema } from '@utils/aquarius'
|
||||
import { ShaclSchemaField } from '@context/MarketMetadata/_shaclType'
|
||||
|
||||
import { capitalizeFirstLetter } from '@utils/textTransform'
|
||||
|
||||
function getMinMax(valueField: string, field: ShaclSchemaField) {
|
||||
let min = field.minLength
|
||||
const max = field.maxLength
|
||||
|
||||
// TODO: remove once all fields in schema has minLength property
|
||||
if (!min) {
|
||||
switch (valueField) {
|
||||
case 'name':
|
||||
min = 4
|
||||
break
|
||||
case 'description':
|
||||
min = 10
|
||||
break
|
||||
case 'author':
|
||||
min = 1
|
||||
break
|
||||
case 'tags':
|
||||
min = 1
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
minLength: min,
|
||||
maxLength: max
|
||||
}
|
||||
}
|
||||
|
||||
export async function validateFieldSchaclSchema(
|
||||
keyField: string,
|
||||
valueField: string,
|
||||
value: any,
|
||||
createError: any
|
||||
): Promise<any> {
|
||||
const schemaField: any = await retrieveShaclSchema()
|
||||
const fieldSchema: ShaclSchemaField = schemaField[keyField][valueField]
|
||||
const { minLength, maxLength } = getMinMax(valueField, fieldSchema)
|
||||
|
||||
// TODO: add minLength when integrated in endpoint
|
||||
if (value.length < minLength) {
|
||||
return createError({
|
||||
message: `${capitalizeFirstLetter(
|
||||
valueField
|
||||
)} must be at least ${minLength} characters`
|
||||
})
|
||||
} else if (value.length > maxLength) {
|
||||
return createError({
|
||||
message: `${capitalizeFirstLetter(
|
||||
valueField
|
||||
)} must have maximum ${maxLength} characters`
|
||||
})
|
||||
} else {
|
||||
return value
|
||||
}
|
||||
}
|
@ -1,12 +1,7 @@
|
||||
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'
|
||||
import { validateFieldSchaclSchema } from '@utils/schaclSchema'
|
||||
|
||||
// TODO: conditional validation
|
||||
// e.g. when algo is selected, Docker image is required
|
||||
@ -19,97 +14,57 @@ const validationMetadata = {
|
||||
.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
|
||||
}
|
||||
const keyField = path.split('.')[0]
|
||||
const valueField = path.split('.')[1]
|
||||
|
||||
return await validateFieldSchaclSchema(
|
||||
keyField,
|
||||
valueField,
|
||||
value,
|
||||
createError
|
||||
)
|
||||
}),
|
||||
description: 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 < 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
|
||||
}
|
||||
const keyField = path.split('.')[0]
|
||||
const valueField = path.split('.')[1]
|
||||
|
||||
return await validateFieldSchaclSchema(
|
||||
keyField,
|
||||
valueField,
|
||||
value,
|
||||
createError
|
||||
)
|
||||
}),
|
||||
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
|
||||
}
|
||||
const keyField = path.split('.')[0]
|
||||
const valueField = path.split('.')[1]
|
||||
|
||||
return await validateFieldSchaclSchema(
|
||||
keyField,
|
||||
valueField,
|
||||
value,
|
||||
createError
|
||||
)
|
||||
}),
|
||||
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
|
||||
}
|
||||
if (!value) return
|
||||
const keyField = path.split('.')[0]
|
||||
const valueField = path.split('.')[1]
|
||||
|
||||
return await validateFieldSchaclSchema(
|
||||
keyField,
|
||||
valueField,
|
||||
value,
|
||||
createError
|
||||
)
|
||||
}),
|
||||
termsAndConditions: Yup.boolean()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user