@globalbrain/sefirot 3.13.1 → 3.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/components/STrans.vue +13 -0
- package/lib/composables/Lang.ts +34 -0
- package/lib/validation/Rule.ts +30 -0
- package/lib/validation/rules/index.ts +24 -24
- package/lib/validation/rules/required.ts +11 -5
- package/lib/validation/validators/index.ts +1 -0
- package/lib/validation/validators/required.ts +25 -0
- package/package.json +1 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { inject, provide } from 'vue'
|
|
2
|
+
|
|
3
|
+
export type Lang = 'en' | 'ja'
|
|
4
|
+
|
|
5
|
+
export interface Trans<T> {
|
|
6
|
+
t: T
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface TransMessages<T> {
|
|
10
|
+
en: T
|
|
11
|
+
ja: T
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const SefirotLangKey = 'sefirot-lang-key'
|
|
15
|
+
|
|
16
|
+
export function provideLang(lang: Lang) {
|
|
17
|
+
provide(SefirotLangKey, lang)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function useLang(): Lang {
|
|
21
|
+
// Doing `||` check here because for some reason it doesn't return
|
|
22
|
+
// the default value in tests but becomes `undefined`.
|
|
23
|
+
return inject(SefirotLangKey, 'en') || 'en'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function useTrans<T>(messages: TransMessages<T>): Trans<T> {
|
|
27
|
+
const lang = useLang()
|
|
28
|
+
|
|
29
|
+
const t = messages[lang]
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
t
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type ValidationRuleWithParams } from '@vuelidate/core'
|
|
2
|
+
import { type MessageProps as VMessageProps, helpers } from '@vuelidate/validators'
|
|
3
|
+
import { type Lang, useLang } from '../composables/Lang'
|
|
4
|
+
|
|
5
|
+
export interface RuleOptions {
|
|
6
|
+
optional?: boolean
|
|
7
|
+
message(params: MessageProps): string
|
|
8
|
+
validation(value: unknown): boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface MessageProps extends VMessageProps {
|
|
12
|
+
lang: Lang
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createRule(
|
|
16
|
+
options: RuleOptions
|
|
17
|
+
): ValidationRuleWithParams {
|
|
18
|
+
const lang = useLang()
|
|
19
|
+
|
|
20
|
+
return helpers.withMessage(
|
|
21
|
+
(params) => {
|
|
22
|
+
return options.message({ ...params, lang })
|
|
23
|
+
},
|
|
24
|
+
(value) => {
|
|
25
|
+
return options.optional && !helpers.req(value)
|
|
26
|
+
? true
|
|
27
|
+
: options.validation(value)
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
}
|
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
export { and, not, or } from '@vuelidate/validators'
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
11
|
-
export
|
|
12
|
-
export
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
export
|
|
16
|
-
export
|
|
17
|
-
export
|
|
18
|
-
export
|
|
19
|
-
export
|
|
20
|
-
export
|
|
21
|
-
export
|
|
22
|
-
export
|
|
23
|
-
export
|
|
24
|
-
export
|
|
25
|
-
export
|
|
2
|
+
export { checked } from './checked'
|
|
3
|
+
export { decimal } from './decimal'
|
|
4
|
+
export { decimalOrHyphen } from './decimalOrHyphen'
|
|
5
|
+
export { email } from './email'
|
|
6
|
+
export { fileExtension } from './fileExtension'
|
|
7
|
+
export { hms } from './hms'
|
|
8
|
+
export { maxFileSize } from './maxFileSize'
|
|
9
|
+
export { maxLength } from './maxLength'
|
|
10
|
+
export { maxTotalFileSize } from './maxTotalFileSize'
|
|
11
|
+
export { maxValue } from './maxValue'
|
|
12
|
+
export { minLength } from './minLength'
|
|
13
|
+
export { minValue } from './minValue'
|
|
14
|
+
export { month } from './month'
|
|
15
|
+
export { negativeInteger } from './negativeInteger'
|
|
16
|
+
export { positiveInteger } from './positiveInteger'
|
|
17
|
+
export { required } from './required'
|
|
18
|
+
export { requiredHms } from './requiredHms'
|
|
19
|
+
export { requiredIf } from './requiredIf'
|
|
20
|
+
export { requiredYmd } from './requiredYmd'
|
|
21
|
+
export { rule } from './rule'
|
|
22
|
+
export { url } from './url'
|
|
23
|
+
export { ymd } from './ymd'
|
|
24
|
+
export { zeroOrNegativeInteger } from './zeroOrNegativeInteger'
|
|
25
|
+
export { zeroOrPositiveInteger } from './zeroOrPositiveInteger'
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createRule } from '../Rule'
|
|
2
|
+
import { required as baseRequired } from '../validators'
|
|
3
|
+
|
|
4
|
+
export const message = {
|
|
5
|
+
en: 'The field is required.',
|
|
6
|
+
ja: 'この項目は必須です。'
|
|
7
|
+
}
|
|
2
8
|
|
|
3
9
|
export function required(msg?: string) {
|
|
4
|
-
return
|
|
5
|
-
() => msg ??
|
|
6
|
-
baseRequired
|
|
7
|
-
)
|
|
10
|
+
return createRule({
|
|
11
|
+
message: ({ lang }) => msg ?? message[lang],
|
|
12
|
+
validation: (value) => baseRequired(value)
|
|
13
|
+
})
|
|
8
14
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isArray, isNullish, isString } from '../../support/Utils'
|
|
2
|
+
|
|
3
|
+
export function required(value: unknown): boolean {
|
|
4
|
+
if (isArray(value)) {
|
|
5
|
+
return !!value.length
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (isNullish(value)) {
|
|
9
|
+
return false
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (value === false) {
|
|
13
|
+
return true
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (value instanceof Date) {
|
|
17
|
+
return !Number.isNaN(value.getTime())
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (isString(value)) {
|
|
21
|
+
value = value.trim()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return !!String(value).length
|
|
25
|
+
}
|