Skip to content

Commit 2d2b8fc

Browse files
committed
wip fix tests
1 parent c42efcc commit 2d2b8fc

File tree

9 files changed

+142
-148
lines changed

9 files changed

+142
-148
lines changed

packages/core/src/fields/non-null-graphql.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ export function resolveDbNullable (
1717
return true
1818
}
1919

20-
function shouldAddValidation (
21-
db?: { isNullable?: boolean },
22-
validation?: unknown
23-
) {
24-
if (db?.isNullable === false) return true
25-
if (validation !== undefined) return true
26-
return false
27-
}
28-
2920
export function makeValidateHook <ListTypeInfo extends BaseListTypeInfo> (
3021
meta: FieldData,
3122
config: {
@@ -40,22 +31,28 @@ export function makeValidateHook <ListTypeInfo extends BaseListTypeInfo> (
4031
},
4132
validation?: {
4233
isRequired?: boolean
34+
[key: string]: unknown
4335
},
4436
},
4537
f?: ValidateFieldHook<ListTypeInfo, 'create' | 'update' | 'delete', ListTypeInfo['fields']>
4638
) {
4739
const dbNullable = resolveDbNullable(config.validation, config.db)
4840
const mode = dbNullable ? ('optional' as const) : ('required' as const)
41+
const valueRequired = config.validation?.isRequired || !dbNullable
4942

5043
assertReadIsNonNullAllowed(meta, config, dbNullable)
51-
const addValidation = shouldAddValidation(config.db, config.validation)
44+
const addValidation = config.db?.isNullable === false || config.validation?.isRequired
5245
if (addValidation) {
5346
const validate = async function (args) {
5447
const { operation, addValidationError, resolvedData } = args
55-
if (operation !== 'delete') {
56-
const value = resolvedData[meta.fieldKey]
57-
if ((config.validation?.isRequired || dbNullable === false) && value === null) {
58-
addValidationError(`Missing value`)
48+
49+
if (valueRequired) {
50+
const value = resolvedData?.[meta.fieldKey]
51+
if (
52+
(operation === 'create' && value === undefined)
53+
|| ((operation === 'create' || operation === 'update') && (value === null))
54+
) {
55+
addValidationError(`missing value`)
5956
}
6057
}
6158

@@ -70,7 +67,7 @@ export function makeValidateHook <ListTypeInfo extends BaseListTypeInfo> (
7067

7168
return {
7269
mode,
73-
validate: undefined
70+
validate: f
7471
}
7572
}
7673

packages/core/src/fields/types/bigInt/index.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,27 @@ export function bigInt <ListTypeInfo extends BaseListTypeInfo> (
3939
const {
4040
isIndexed,
4141
defaultValue: _defaultValue,
42-
validation: validation_,
42+
validation,
4343
} = config
4444

45+
const v = {
46+
isRequired: validation?.isRequired ?? false,
47+
min: validation?.min ?? MIN_INT,
48+
max: validation?.max ?? MAX_INT,
49+
}
50+
4551
return (meta) => {
4652
const defaultValue = _defaultValue ?? null
4753
const hasAutoIncDefault =
4854
typeof defaultValue == 'object' &&
4955
defaultValue !== null &&
5056
defaultValue.kind === 'autoincrement'
5157

52-
const isNullable = resolveDbNullable(validation_, config.db)
53-
5458
if (hasAutoIncDefault) {
5559
if (meta.provider === 'sqlite' || meta.provider === 'mysql') {
5660
throw new Error(`${meta.listKey}.${meta.fieldKey} specifies defaultValue: { kind: 'autoincrement' }, this is not supported on ${meta.provider}`)
5761
}
62+
const isNullable = resolveDbNullable(v, config.db)
5863
if (isNullable !== false) {
5964
throw new Error(
6065
`${meta.listKey}.${meta.fieldKey} specifies defaultValue: { kind: 'autoincrement' } but doesn't specify db.isNullable: false.\n` +
@@ -64,44 +69,39 @@ export function bigInt <ListTypeInfo extends BaseListTypeInfo> (
6469
}
6570
}
6671

67-
const validation = {
68-
isRequired: validation_?.isRequired ?? false,
69-
min: validation_?.min ?? MIN_INT,
70-
max: validation_?.max ?? MAX_INT,
71-
}
72-
7372
for (const type of ['min', 'max'] as const) {
74-
if (validation[type] > MAX_INT || validation[type] < MIN_INT) {
75-
throw new Error(`${meta.listKey}.${meta.fieldKey} specifies validation.${type}: ${validation[type]} which is outside of the range of a 64bit signed integer(${MIN_INT}n - ${MAX_INT}n) which is not allowed`)
73+
if (v[type] > MAX_INT || v[type] < MIN_INT) {
74+
throw new Error(`${meta.listKey}.${meta.fieldKey} specifies validation.${type}: ${v[type]} which is outside of the range of a 64bit signed integer(${MIN_INT}n - ${MAX_INT}n) which is not allowed`)
7675
}
7776
}
78-
if (validation.min > validation.max) {
77+
if (v.min > v.max) {
7978
throw new Error(`${meta.listKey}.${meta.fieldKey} specifies a validation.max that is less than the validation.min, and therefore has no valid options`)
8079
}
8180

81+
const hasAdditionalValidation = v.min !== undefined || v.max !== undefined
8282
const {
8383
mode,
8484
validate,
85-
} = makeValidateHook(meta, config, ({ resolvedData, operation, addValidationError }) => {
85+
} = makeValidateHook(meta, config, hasAdditionalValidation ? ({ resolvedData, operation, addValidationError }) => {
8686
if (operation === 'delete') return
8787

8888
const value = resolvedData[meta.fieldKey]
8989
if (typeof value === 'number') {
90-
if (validation?.min !== undefined && value < validation.min) {
91-
addValidationError(`value must be greater than or equal to ${validation.min}`)
90+
if (v?.min !== undefined && value < v.min) {
91+
addValidationError(`value must be greater than or equal to ${v.min}`)
9292
}
9393

94-
if (validation?.max !== undefined && value > validation.max) {
95-
addValidationError(`value must be less than or equal to ${validation.max}`)
94+
if (v?.max !== undefined && value > v.max) {
95+
addValidationError(`value must be less than or equal to ${v.max}`)
9696
}
9797
}
98-
})
98+
} : undefined)
9999

100100
return fieldType({
101101
kind: 'scalar',
102102
mode,
103103
scalar: 'BigInt',
104-
// This will resolve to 'index' if the boolean is true, otherwise other values - false will be converted to undefined
104+
// this will resolve to 'index' if the boolean is true, otherwise other values - false will be converted to undefined
105105
index: isIndexed === true ? 'index' : isIndexed || undefined,
106106
default:
107107
typeof defaultValue === 'bigint'
@@ -143,9 +143,9 @@ export function bigInt <ListTypeInfo extends BaseListTypeInfo> (
143143
getAdminMeta () {
144144
return {
145145
validation: {
146-
min: validation.min.toString(),
147-
max: validation.max.toString(),
148-
isRequired: validation.isRequired,
146+
min: v.min.toString(),
147+
max: v.max.toString(),
148+
isRequired: v.isRequired,
149149
},
150150
defaultValue: typeof defaultValue === 'bigint' ? defaultValue.toString() : defaultValue,
151151
}

packages/core/src/fields/types/calendarDay/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ export type CalendarDayFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
2525
}
2626
}
2727

28-
export const calendarDay =
29-
<ListTypeInfo extends BaseListTypeInfo>({
28+
export function calendarDay <ListTypeInfo extends BaseListTypeInfo>(config: CalendarDayFieldConfig<ListTypeInfo> = {}): FieldTypeFunc<ListTypeInfo> {
29+
const {
3030
isIndexed,
3131
validation,
3232
defaultValue,
33-
...config
34-
}: CalendarDayFieldConfig<ListTypeInfo> = {}): FieldTypeFunc<ListTypeInfo> =>
35-
meta => {
33+
} = config
34+
return (meta) => {
3635
if (typeof defaultValue === 'string') {
3736
try {
3837
graphql.CalendarDay.graphQLType.parseValue(defaultValue)
@@ -126,6 +125,7 @@ export const calendarDay =
126125
},
127126
})
128127
}
128+
}
129129

130130
function dateStringToDateObjectInUTC (value: string) {
131131
return new Date(`${value}T00:00Z`)

packages/core/src/fields/types/float/index.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ export type FloatFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
2727
}
2828
}
2929

30-
export const float =
31-
<ListTypeInfo extends BaseListTypeInfo>({
32-
isIndexed,
33-
validation,
30+
export function float <ListTypeInfo extends BaseListTypeInfo>(config: FloatFieldConfig<ListTypeInfo> = {}): FieldTypeFunc<ListTypeInfo> {
31+
const {
3432
defaultValue,
35-
...config
36-
}: FloatFieldConfig<ListTypeInfo> = {}): FieldTypeFunc<ListTypeInfo> =>
37-
meta => {
33+
isIndexed,
34+
validation: v = {},
35+
} = config
36+
return (meta) => {
3837
if (
3938
defaultValue !== undefined &&
4039
(typeof defaultValue !== 'number' || !Number.isFinite(defaultValue))
@@ -43,45 +42,46 @@ export const float =
4342
}
4443

4544
if (
46-
validation?.min !== undefined &&
47-
(typeof validation.min !== 'number' || !Number.isFinite(validation.min))
45+
v.min !== undefined &&
46+
(typeof v.min !== 'number' || !Number.isFinite(v.min))
4847
) {
49-
throw new Error(`${meta.listKey}.${meta.fieldKey} specifies validation.min: ${validation.min} but it must be a valid finite number`)
48+
throw new Error(`${meta.listKey}.${meta.fieldKey} specifies validation.min: ${v.min} but it must be a valid finite number`)
5049
}
5150

5251
if (
53-
validation?.max !== undefined &&
54-
(typeof validation.max !== 'number' || !Number.isFinite(validation.max))
52+
v.max !== undefined &&
53+
(typeof v.max !== 'number' || !Number.isFinite(v.max))
5554
) {
56-
throw new Error(`${meta.listKey}.${meta.fieldKey} specifies validation.max: ${validation.max} but it must be a valid finite number`)
55+
throw new Error(`${meta.listKey}.${meta.fieldKey} specifies validation.max: ${v.max} but it must be a valid finite number`)
5756
}
5857

5958
if (
60-
validation?.min !== undefined &&
61-
validation?.max !== undefined &&
62-
validation.min > validation.max
59+
v.min !== undefined &&
60+
v.max !== undefined &&
61+
v.min > v.max
6362
) {
6463
throw new Error(`${meta.listKey}.${meta.fieldKey} specifies a validation.max that is less than the validation.min, and therefore has no valid options`)
6564
}
6665

66+
const hasAdditionalValidation = v.min !== undefined || v.max !== undefined
6767
const {
6868
mode,
6969
validate,
70-
} = makeValidateHook(meta, config, ({ resolvedData, operation, addValidationError }) => {
70+
} = makeValidateHook(meta, config, hasAdditionalValidation ? ({ resolvedData, operation, addValidationError }) => {
7171
if (operation === 'delete') return
7272

7373
const value = resolvedData[meta.fieldKey]
7474
if (typeof value === 'number') {
75-
if (validation?.max !== undefined && value > validation.max) {
76-
addValidationError(`value must be less than or equal to ${validation.max}`
75+
if (v.max !== undefined && value > v.max) {
76+
addValidationError(`value must be less than or equal to ${v.max}`
7777
)
7878
}
7979

80-
if (validation?.min !== undefined && value < validation.min) {
81-
addValidationError(`value must be greater than or equal to ${validation.min}`)
80+
if (v.min !== undefined && value < v.min) {
81+
addValidationError(`value must be greater than or equal to ${v.min}`)
8282
}
8383
}
84-
})
84+
} : undefined)
8585

8686
return fieldType({
8787
kind: 'scalar',
@@ -95,8 +95,7 @@ export const float =
9595
...config,
9696
hooks: mergeFieldHooks({ validate }, config.hooks),
9797
input: {
98-
uniqueWhere:
99-
isIndexed === 'unique' ? { arg: graphql.arg({ type: graphql.Float }) } : undefined,
98+
uniqueWhere: isIndexed === 'unique' ? { arg: graphql.arg({ type: graphql.Float }) } : undefined,
10099
where: {
101100
arg: graphql.arg({ type: filters[meta.provider].Float[mode] }),
102101
resolve: mode === 'optional' ? filters.resolveCommon : undefined,
@@ -124,12 +123,13 @@ export const float =
124123
getAdminMeta () {
125124
return {
126125
validation: {
127-
min: validation?.min || null,
128-
max: validation?.max || null,
129-
isRequired: validation?.isRequired ?? false,
126+
isRequired: v.isRequired ?? false,
127+
min: v.min ?? null,
128+
max: v.max ?? null,
130129
},
131130
defaultValue: defaultValue ?? null,
132131
}
133132
},
134133
})
135134
}
135+
}

0 commit comments

Comments
 (0)