sanity-plugin-internationalized-array 1.0.0 → 1.1.1

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.
@@ -1,111 +0,0 @@
1
- import {
2
- CustomValidatorResult,
3
- defineField,
4
- FieldDefinition,
5
- Rule,
6
- SchemaType,
7
- ValidationError,
8
- } from 'sanity'
9
-
10
- import InternationalizedArrayInput from './components/InternationalizedArrayInput'
11
- import {AllowedType, ArrayConfig, Language, Value} from './types'
12
-
13
- const CONFIG_DEFAULT = {name: `title`, type: `string` as AllowedType, languages: []}
14
-
15
- export function internationalizedArray(config: ArrayConfig = CONFIG_DEFAULT): FieldDefinition {
16
- const {name, type, languages} = config
17
-
18
- const configValidation = Array.isArray(config?.validation)
19
- ? config.validation
20
- : [config?.validation]
21
-
22
- return defineField({
23
- name,
24
- title: config?.title ?? undefined,
25
- group: config?.group ?? undefined,
26
- hidden: config?.hidden ?? undefined,
27
- readOnly: config?.readOnly ?? undefined,
28
- type: 'array',
29
- components: {input: InternationalizedArrayInput},
30
- options: {languages},
31
- of: [
32
- {
33
- type: 'object',
34
- fields: [
35
- {
36
- name: 'value',
37
- type,
38
- },
39
- ],
40
- preview: {
41
- select: {title: 'value', key: '_key'},
42
- prepare(select) {
43
- const {title, key} = select as Record<string, string>
44
-
45
- return {
46
- title,
47
- subtitle: key.toUpperCase(),
48
- }
49
- },
50
- },
51
- },
52
- ],
53
- // @ts-ignore
54
- validation: (rule: Rule) => {
55
- const rules = [] as Rule[]
56
-
57
- rules.push(
58
- rule.custom<Value[]>((value, context) => {
59
- const {languages: contextLanguages}: {languages: Language[]} =
60
- context?.type?.options ?? {}
61
- const nonLanguageKeys = value?.length
62
- ? value.filter(
63
- (item) => !contextLanguages.find((language) => item._key === language.id)
64
- )
65
- : []
66
- if (nonLanguageKeys.length) {
67
- return {
68
- message: `Array item keys must be valid languages registered to the field type`,
69
- paths: nonLanguageKeys.map((item) => [{_key: item._key}]),
70
- }
71
- }
72
-
73
- // Ensure there's no duplicate `language` fields
74
- type KeyedValues = {
75
- [key: string]: Value[]
76
- }
77
-
78
- const valuesByLanguage = value?.length
79
- ? value
80
- .filter((item) => Boolean(item?._key))
81
- .reduce((acc, cur) => {
82
- if (acc[cur._key]) {
83
- return {...acc, [cur._key]: [...acc[cur._key], cur]}
84
- }
85
- return {
86
- ...acc,
87
- [cur._key]: [cur],
88
- }
89
- }, {} as KeyedValues)
90
- : {}
91
- const duplicateValues = Object.values(valuesByLanguage)
92
- .filter((item) => item?.length > 1)
93
- .flat()
94
- if (duplicateValues.length) {
95
- return {
96
- message: 'There can only be one field per language',
97
- paths: duplicateValues.map((item) => [{_key: item._key}]),
98
- }
99
- }
100
- return true
101
- })
102
- )
103
-
104
- if (languages?.length) {
105
- rules.push(rule.max(languages.length))
106
- }
107
-
108
- return [...rules, ...configValidation].filter(Boolean)
109
- },
110
- })
111
- }