Skip to content

Commit

Permalink
Merge pull request #871 from 3YOURMIND/naruto/pre-upgrade/properly-ty…
Browse files Browse the repository at this point in the history
…pe-defaults-in-make-props

fix(makeProps): Properly Handle Types for defaults
  • Loading branch information
carsoli authored Jan 15, 2024
2 parents 6069443 + f0ab895 commit 40073d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
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

0 comments on commit 40073d9

Please sign in to comment.