TypeAssert.ts 757 B

123456789101112131415161718192021222324252627
  1. import type { AssertRule } from '~/types/interfaces'
  2. import { useI18n } from 'vue-i18n'
  3. import ValidationUtils from '~/services/utils/validationUtils'
  4. export class TypeAssert implements AssertRule {
  5. supports(key: string): boolean {
  6. return key === 'type'
  7. }
  8. createRule(criteria: string): (value: unknown) => true | string {
  9. const validationUtils = new ValidationUtils()
  10. const { t } = useI18n()
  11. if (criteria === 'email') {
  12. return (email: unknown) =>
  13. (typeof email === 'string' && validationUtils.validEmail(email)) ||
  14. t('email_error')
  15. }
  16. if (criteria === 'integer') {
  17. return (value: unknown) =>
  18. Number.isInteger(value as number) || t('need_to_be_integer')
  19. }
  20. return () => true
  21. }
  22. }