-
Notifications
You must be signed in to change notification settings - Fork 388
/
array.test.ts
198 lines (185 loc) · 5.82 KB
/
array.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
* use of ts-expect-error serves the same purpose - TypeScript is the testrunner here
*/
import {type ArrayDefinition, type BooleanDefinition} from '../definition'
import {defineArrayMember, defineField, defineType} from '../types'
describe('array types', () => {
describe('defineType', () => {
it('should define string array schema', () => {
const arrayDef = defineType({
type: 'array',
name: 'custom-array',
title: 'Custom array',
description: 'Description',
initialValue: () => Promise.resolve(['string']),
validation: (Rule) => [
Rule.required()
.unique()
.min(1)
.max(10)
.length(10)
// concession: making arrayDefinition generic to its value type
// is possible but we would have to make the 'of' property optional on all types in the defineType signature.
// That seemed less ideal than having to type custom value here,
// so the implementation was dropped
.custom((value?: string[]) =>
value?.length === 2 && value[0].toLowerCase() == 'yolo' ? 'Error' : true,
)
.warning(),
// @ts-expect-error greaterThan does not exist on ArrayRule
Rule.greaterThan(5).error(),
],
hidden: () => false,
readOnly: () => false,
of: [
{type: 'string'},
{type: 'string', name: 'suffix'},
{
type: 'string',
name: 'suffix2',
title: 'Titled',
// when we dont use defineArrayMember, validation cannot be inferred and falls back to generic rule
// @ts-expect-error value is unknown
validation: (Rule) => Rule.custom((value) => (value?.toLowerCase() ? true : 'Error')),
},
defineArrayMember({
type: 'string',
name: 'suffix2',
title: 'Titled',
validation: (Rule) => Rule.custom((value) => (value?.toLowerCase() ? true : 'Error')),
}),
],
options: {
layout: 'grid',
sortable: true,
list: [{value: 'A', title: 'An entry'}],
modal: {
type: 'popover',
width: 'auto',
},
//allowUnknownOptions: true,
},
})
let assignableToArray: ArrayDefinition = arrayDef
assignableToArray = defineType(assignableToArray)
// @ts-expect-error string is not assignable to boolean
const notAssignableToBoolean: BooleanDefinition = arrayDef
})
it('should define object array schema', () => {
const arrayDef = defineType({
type: 'array',
name: 'custom-array',
title: 'Custom array',
description: 'Description',
initialValue: () => Promise.resolve([{title: 'thing'}]),
validation: (Rule) => [
Rule.required()
.unique()
.min(1)
.max(10)
.length(10)
.custom((value) => (value?.length === 2 ? 'Error' : true))
.warning(),
],
hidden: () => false,
readOnly: () => false,
// type inference here is not great
of: [
defineArrayMember({
type: 'object',
name: 'inline-object-via-define',
fields: [
{
type: 'string',
name: 'field',
},
],
}),
{
type: 'object',
name: 'inline-object',
fields: [
{
type: 'string',
name: 'field',
},
],
},
{
type: 'reference',
to: [{type: 'castMember'}, {type: 'crewMember'}],
},
{
type: 'aliased-type',
},
],
options: {
layout: 'grid',
sortable: true,
modal: {
type: 'popover',
width: 'auto',
},
list: [{value: {prop: 'string'}, title: 'An entry'}],
//@ts-expect-error unknown prop
allowUnknownOptions: false,
},
})
let assignableToArray: ArrayDefinition = arrayDef
assignableToArray = defineType(assignableToArray)
// @ts-expect-error string is not assignable to boolean
const notAssignableToBoolean: BooleanDefinition = arrayDef
})
})
it('should allow inline object def in array-of-object', () => {
defineType({
type: 'array',
name: 'custom-array',
of: [
{
type: 'object',
name: 'inline-object',
title: 'Custom title',
fields: [
{
type: 'boolean',
name: 'checkYourself',
title: 'Check me',
options: {
layout: 'switch',
},
},
],
},
],
})
})
it('should support tag layout', () => {
const arrayField: ArrayDefinition = defineField({
type: 'array',
name: 'arrayTagList',
description: 'array with tag-list',
of: [{type: 'string'}],
options: {
list: ['1'],
layout: 'tags',
},
})
})
it('should support Rule.valueOfField calls inside defineField', () => {
const arrayField: ArrayDefinition = defineField({
type: 'array',
name: 'defineField-defined',
description: 'field defined with defineField, containing validation using Rule.valueOfField',
of: [{type: 'string'}],
validation: (Rule) => {
const fieldRef = Rule.valueOfField('some-other-field')
return Rule.min(fieldRef).max(fieldRef).length(fieldRef)
},
})
})
})
export {}