Browse Source

fix(nc-gui): convert old validators to the latest format

pull/4401/head
Wing-Kam Wong 2 years ago
parent
commit
a01337cacf
  1. 112
      packages/nc-gui/utils/validation.ts

112
packages/nc-gui/utils/validation.ts

@ -3,80 +3,54 @@ import { getI18n } from '~/plugins/a.i18n'
export const isEmail = (v: string) => export const isEmail = (v: string) =>
/^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i.test(v) /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i.test(v)
export function validateTableName(v: string, isGQL = false) { export const validateTableName = {
const { t } = getI18n().global validator: (_: unknown, value: string) => {
return new Promise((resolve, reject) => {
const { t } = getI18n().global
if (!v) { if (!value) {
// return 'Table name required' // return 'Table name required'
return t('msg.error.tableNameRequired') return reject(new Error(t('msg.error.tableNameRequired')))
} }
// GraphQL naming convention // exclude . / \
// http://spec.graphql.org/June2018/#Name // rest all characters allowed
// https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/acreldb/n0rfg6x1shw0ppn1cwhco6yn09f7.htm#:~:text=By%20default%2C%20MySQL%20encloses%20column,not%20truncate%20a%20longer%20name.
if (isGQL) { const m = value.match(/[./\\]/g)
if (/^[_A-Za-z][_0-9A-Za-z]*$/.test(v)) { if (m) {
return true // return `Following characters are not allowed ${m.map((c) => JSON.stringify(c)).join(', ')}`
} return reject(
new Error(`${t('msg.error.followingCharactersAreNotAllowed')} ${m.map((c) => JSON.stringify(c)).join(', ')}`),
if (/^[^_A-Za-z]/.test(v)) { )
// return 'Name should start with an alphabet or _' }
return t('msg.error.nameShouldStartWithAnAlphabetOr_') return resolve(true)
} })
const m = v.match(/[^_A-Za-z\d]/g) },
if (m) {
// return `Following characters are not allowed ${m.map((c) => JSON.stringify(c)).join(', ')}`
return `${t('msg.error.followingCharactersAreNotAllowed')} ${m.map((c) => JSON.stringify(c)).join(', ')}`
}
} else {
// exclude . / \
// rest all characters allowed
// https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/acreldb/n0rfg6x1shw0ppn1cwhco6yn09f7.htm#:~:text=By%20default%2C%20MySQL%20encloses%20column,not%20truncate%20a%20longer%20name.
const m = v.match(/[./\\]/g)
if (m) {
// return `Following characters are not allowed ${m.map((c) => JSON.stringify(c)).join(', ')}`
return `${t('msg.error.followingCharactersAreNotAllowed')} ${m.map((c) => JSON.stringify(c)).join(', ')}`
}
return true
}
} }
export function validateColumnName(v: string, isGQL = false) { export const validateColumnName = {
const { t } = getI18n().global validator: (_: unknown, value: string) => {
if (!v) { return new Promise((resolve, reject) => {
// return 'Column name required' const { t } = getI18n().global
return t('msg.error.columnNameRequired')
}
// GraphQL naming convention if (!value) {
// http://spec.graphql.org/June2018/#Name // return 'Column name required'
if (isGQL) { return reject(new Error(t('msg.error.columnNameRequired')))
if (/^[_A-Za-z][_0-9A-Za-z]*$/.test(v)) { }
return true
} // exclude . / \
// rest all characters allowed
if (/^[^_A-Za-z]/.test(v)) { // https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/acreldb/n0rfg6x1shw0ppn1cwhco6yn09f7.htm#:~:text=By%20default%2C%20MySQL%20encloses%20column,not%20truncate%20a%20longer%20name.
// return 'Name should start with an alphabet or _' const m = value.match(/[./\\]/g)
return t('msg.error.nameShouldStartWithAnAlphabetOr_') if (m) {
} // return `Following characters are not allowed ${m.map((c) => JSON.stringify(c)).join(', ')}`
const m = v.match(/[^_A-Za-z\d]/g) return reject(
if (m) { new Error(`${t('msg.error.followingCharactersAreNotAllowed')} ${m.map((c) => JSON.stringify(c)).join(', ')}`),
// return `Following characters are not allowed ${m.map((c) => JSON.stringify(c)).join(', ')}` )
return `${t('msg.error.followingCharactersAreNotAllowed')} ${m.map((c) => JSON.stringify(c)).join(', ')}` }
} return resolve(true)
} else { })
// exclude . / \ },
// rest all characters allowed
// https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/acreldb/n0rfg6x1shw0ppn1cwhco6yn09f7.htm#:~:text=By%20default%2C%20MySQL%20encloses%20column,not%20truncate%20a%20longer%20name.
const m = v.match(/[./\\]/g)
if (m) {
// return `Following characters are not allowed ${m.map((c) => JSON.stringify(c)).join(', ')}`
return `${t('msg.error.followingCharactersAreNotAllowed')} ${m.map((c) => JSON.stringify(c)).join(', ')}`
}
return true
}
} }
export const projectTitleValidator = { export const projectTitleValidator = {

Loading…
Cancel
Save