Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(makeProps): Properly Handle Types for defaults #871

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/kotti-ui/source/make-props.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,8 @@ describe('never', () => {
null,
undefined,
)
expect(prop.default.description).toBe('NEVER')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((prop.default as any)?.description).toBe('NEVER')
})

it('can’t specify “z.never().nullable()”', () => {
Expand Down
29 changes: 19 additions & 10 deletions packages/kotti-ui/source/make-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ const propValidator = <SCHEMA extends z.ZodTypeAny>({
// HACK: 'error' in result is necessary as `ts-jest` doesn’t see that result.success was already properly checked to be falsy and that error now exists
if ('error' in result) console.error(result.error)

if (Array.isArray(value)) console.table(cloneDeep(value))
else console.log(cloneDeep(value))
const clonedValue = cloneDeep(value)
if (Array.isArray(value)) console.table(clonedValue)
else console.log(clonedValue)

console.trace()

Expand All @@ -57,8 +58,9 @@ const propValidator = <SCHEMA extends z.ZodTypeAny>({

console.error(error)

if (Array.isArray(value)) console.table(cloneDeep(value))
else console.log(cloneDeep(value))
const clonedValue = cloneDeep(value)
if (Array.isArray(value)) console.table(clonedValue)
else console.log(clonedValue)

console.trace()

Expand Down Expand Up @@ -240,8 +242,11 @@ export const makeProps = <PROPS_SCHEMA extends z.ZodObject<z.ZodRawShape>>(
): {
[PROP_NAME in keyof PROPS_SCHEMA['shape']]: Omit<
PropOptions,
'required' | 'type'
'default' | 'required' | 'type'
> & {
default: undefined extends z.input<PROPS_SCHEMA>[PROP_NAME]
? () => z.output<PROPS_SCHEMA>[PROP_NAME]
: undefined
required: undefined extends z.input<PROPS_SCHEMA>[PROP_NAME] ? false : true
type: PropType<z.output<PROPS_SCHEMA>[PROP_NAME]>
}
Expand All @@ -267,7 +272,11 @@ export const makeProps = <PROPS_SCHEMA extends z.ZodObject<z.ZodRawShape>>(
}),
}

if (!isNever) {
if (isNever) {
// HACK: for the KtFields because we are constrained by VueTypes
propDefinition.default = NEVER
propDefinition.type = Symbol
} else {
const vuePropTypes = uniq(
[...zodTypeSet]
.filter((x) => !ignoredZodTypes.has(x))
Expand Down Expand Up @@ -308,9 +317,6 @@ export const makeProps = <PROPS_SCHEMA extends z.ZodObject<z.ZodRawShape>>(

if (isOptional) propDefinition.default = propSchema._def.defaultValue
else propDefinition.required = true
} else {
propDefinition.default = NEVER
propDefinition.type = Symbol
}

return [propName, propDefinition]
Expand All @@ -319,8 +325,11 @@ export const makeProps = <PROPS_SCHEMA extends z.ZodObject<z.ZodRawShape>>(
) as {
[KEY in keyof PROPS_SCHEMA['shape']]: Omit<
PropOptions,
'required' | 'type'
'default' | 'required' | 'type'
> & {
default: undefined extends z.input<PROPS_SCHEMA>[KEY]
? () => z.output<PROPS_SCHEMA>[KEY]
: undefined
required: undefined extends z.input<PROPS_SCHEMA>[KEY] ? false : true
type: PropType<z.output<PROPS_SCHEMA>[KEY]>
}
Expand Down
Loading