@globalbrain/sefirot 4.54.0 → 4.55.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/blocks/lens/Rule.ts +6 -0
- package/lib/blocks/lens/validation/RuleMapper.ts +3 -0
- package/lib/validation/rules/index.ts +1 -0
- package/lib/validation/rules/mimeTypes.ts +15 -0
- package/lib/validation/validators/index.ts +1 -0
- package/lib/validation/validators/mimeTypes.ts +19 -0
- package/package.json +1 -1
package/lib/blocks/lens/Rule.ts
CHANGED
|
@@ -21,6 +21,7 @@ export type Rule =
|
|
|
21
21
|
| AfterRule
|
|
22
22
|
| AfterOrEqualRule
|
|
23
23
|
| FileExtensionRule
|
|
24
|
+
| MimeTypesRule
|
|
24
25
|
| MaxFileSizeRule
|
|
25
26
|
| EachRule
|
|
26
27
|
|
|
@@ -122,6 +123,11 @@ export interface FileExtensionRule {
|
|
|
122
123
|
extensions: string[]
|
|
123
124
|
}
|
|
124
125
|
|
|
126
|
+
export interface MimeTypesRule {
|
|
127
|
+
type: 'mime_types'
|
|
128
|
+
mimeTypes: string[]
|
|
129
|
+
}
|
|
130
|
+
|
|
125
131
|
export interface MaxFileSizeRule {
|
|
126
132
|
type: 'max_file_size'
|
|
127
133
|
size: string
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
maxFileSize,
|
|
14
14
|
maxLength,
|
|
15
15
|
maxValue,
|
|
16
|
+
mimeTypes,
|
|
16
17
|
minLength,
|
|
17
18
|
minValue,
|
|
18
19
|
negativeInteger,
|
|
@@ -130,6 +131,8 @@ function mapRule(rule: Exclude<Rule, EachRule>): ValidationRuleWithParams | null
|
|
|
130
131
|
return afterOrEqual(resolveDate(rule.date))
|
|
131
132
|
case 'file_extension':
|
|
132
133
|
return fileExtension(rule.extensions)
|
|
134
|
+
case 'mime_types':
|
|
135
|
+
return mimeTypes(rule.mimeTypes)
|
|
133
136
|
case 'max_file_size':
|
|
134
137
|
return maxFileSize(rule.size)
|
|
135
138
|
default: {
|
|
@@ -12,6 +12,7 @@ export { maxFileSize } from './maxFileSize'
|
|
|
12
12
|
export { maxLength } from './maxLength'
|
|
13
13
|
export { maxTotalFileSize } from './maxTotalFileSize'
|
|
14
14
|
export { maxValue } from './maxValue'
|
|
15
|
+
export { mimeTypes } from './mimeTypes'
|
|
15
16
|
export { minLength } from './minLength'
|
|
16
17
|
export { minValue } from './minValue'
|
|
17
18
|
export { month } from './month'
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createRule } from '../Rule'
|
|
2
|
+
import { mimeTypes as baseMimeTypes } from '../validators/mimeTypes'
|
|
3
|
+
|
|
4
|
+
export const message = {
|
|
5
|
+
en: 'The file type is invalid.',
|
|
6
|
+
ja: 'ファイル形式が正しくありません。'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function mimeTypes(patterns: string[], msg?: string) {
|
|
10
|
+
return createRule({
|
|
11
|
+
message: ({ lang }) => msg ?? message[lang],
|
|
12
|
+
optional: true,
|
|
13
|
+
validation: (value) => baseMimeTypes(value, patterns)
|
|
14
|
+
})
|
|
15
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function mimeTypes(value: unknown, patterns: string[]): boolean {
|
|
2
|
+
if (!(value instanceof File)) { return false }
|
|
3
|
+
|
|
4
|
+
// The browser doesn't always report a type (e.g. some drag-and-drop or
|
|
5
|
+
// clipboard sources); defer to the server's content-based check in that case
|
|
6
|
+
// rather than rejecting a possibly-valid file outright.
|
|
7
|
+
if (value.type === '') { return true }
|
|
8
|
+
|
|
9
|
+
const type = value.type.toLowerCase()
|
|
10
|
+
|
|
11
|
+
return patterns.some((pattern) => {
|
|
12
|
+
const p = pattern.toLowerCase()
|
|
13
|
+
|
|
14
|
+
// A `type/*` wildcard (e.g. `image/*`) matches any subtype of that
|
|
15
|
+
// top-level type, mirroring the HTML accept attribute and Laravel's
|
|
16
|
+
// `mimetypes` rule.
|
|
17
|
+
return p.endsWith('/*') ? type.startsWith(p.slice(0, -1)) : type === p
|
|
18
|
+
})
|
|
19
|
+
}
|