27 lines
689 B
TypeScript
27 lines
689 B
TypeScript
import vine from '@vinejs/vine'
|
|
|
|
function isValidJsonValue(value: any): boolean {
|
|
if (value === null) return true
|
|
if (['string', 'number', 'boolean'].includes(typeof value)) return true
|
|
|
|
if (Array.isArray(value)) {
|
|
return value.every(isValidJsonValue)
|
|
}
|
|
|
|
if (typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]') {
|
|
return Object.values(value).every(isValidJsonValue)
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
export const anyJsonRule = vine.createRule((value, _options, field) => {
|
|
if (value === undefined || value === null) {
|
|
return
|
|
}
|
|
|
|
if (!isValidJsonValue(value)) {
|
|
field.report('Not valid JSON in {{ field }}', 'anyJson', field)
|
|
}
|
|
})
|