Skip to content

Commit

Permalink
Fix output type for transformed optional object entries #806
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Aug 29, 2024
1 parent 03f1d86 commit fe4d1fd
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 11 deletions.
1 change: 1 addition & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to the library will be documented in this file.

- Add `nanoid` action to validate Nano IDs (pull request #789)
- Add `undefinedable` and `undefinedableAsync` schema (issue #385)
- Fix invalid output type for transformed optional object entries (issue #806)

## v0.39.0 (August 24, 2024)

Expand Down
10 changes: 9 additions & 1 deletion library/src/schemas/looseObject/looseObject.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { ReadonlyAction } from '../../actions/index.ts';
import type { ReadonlyAction, TransformAction } from '../../actions/index.ts';
import type { SchemaWithPipe } from '../../methods/index.ts';
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
import type { NullishSchema } from '../nullish/index.ts';
Expand Down Expand Up @@ -48,6 +48,12 @@ describe('looseObject', () => {
key4: ObjectSchema<{ key: NumberSchema<undefined> }, never>;
key5: SchemaWithPipe<[StringSchema<undefined>, ReadonlyAction<string>]>;
key6: UndefinedableSchema<StringSchema<undefined>, 'bar'>;
key7: SchemaWithPipe<
[
OptionalSchema<StringSchema<undefined>, never>,
TransformAction<undefined | string, string>,
]
>;
},
undefined
>;
Expand All @@ -61,6 +67,7 @@ describe('looseObject', () => {
key4: { key: number };
key5: string;
key6: string | undefined;
key7?: string;
} & { [key: string]: unknown }
>();
});
Expand All @@ -74,6 +81,7 @@ describe('looseObject', () => {
key4: { key: number };
readonly key5: string;
key6: string;
key7: string;
} & { [key: string]: unknown }
>();
});
Expand Down
10 changes: 9 additions & 1 deletion library/src/schemas/looseObject/looseObjectAsync.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { ReadonlyAction } from '../../actions/index.ts';
import type { ReadonlyAction, TransformAction } from '../../actions/index.ts';
import type { SchemaWithPipe } from '../../methods/index.ts';
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
import type { NullishSchema } from '../nullish/index.ts';
Expand Down Expand Up @@ -53,6 +53,12 @@ describe('looseObjectAsync', () => {
key4: ObjectSchema<{ key: NumberSchema<undefined> }, never>;
key5: SchemaWithPipe<[StringSchema<undefined>, ReadonlyAction<string>]>;
key6: UndefinedableSchema<StringSchema<undefined>, 'bar'>;
key7: SchemaWithPipe<
[
OptionalSchema<StringSchema<undefined>, never>,
TransformAction<undefined | string, string>,
]
>;
},
undefined
>;
Expand All @@ -66,6 +72,7 @@ describe('looseObjectAsync', () => {
key4: { key: number };
key5: string;
key6: string | undefined;
key7?: string;
} & { [key: string]: unknown }
>();
});
Expand All @@ -79,6 +86,7 @@ describe('looseObjectAsync', () => {
key4: { key: number };
readonly key5: string;
key6: string;
key7: string;
} & { [key: string]: unknown }
>();
});
Expand Down
10 changes: 9 additions & 1 deletion library/src/schemas/object/object.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { ReadonlyAction } from '../../actions/index.ts';
import type { ReadonlyAction, TransformAction } from '../../actions/index.ts';
import type { SchemaWithPipe } from '../../methods/index.ts';
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
import type { NullishSchema } from '../nullish/index.ts';
Expand Down Expand Up @@ -47,6 +47,12 @@ describe('object', () => {
key4: ObjectSchema<{ key: NumberSchema<undefined> }, never>;
key5: SchemaWithPipe<[StringSchema<undefined>, ReadonlyAction<string>]>;
key6: UndefinedableSchema<StringSchema<undefined>, 'bar'>;
key7: SchemaWithPipe<
[
OptionalSchema<StringSchema<undefined>, never>,
TransformAction<undefined | string, string>,
]
>;
},
undefined
>;
Expand All @@ -59,6 +65,7 @@ describe('object', () => {
key4: { key: number };
key5: string;
key6: string | undefined;
key7?: string;
}>();
});

Expand All @@ -70,6 +77,7 @@ describe('object', () => {
key4: { key: number };
readonly key5: string;
key6: string;
key7: string;
}>();
});

Expand Down
10 changes: 9 additions & 1 deletion library/src/schemas/object/objectAsync.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { ReadonlyAction } from '../../actions/index.ts';
import type { ReadonlyAction, TransformAction } from '../../actions/index.ts';
import type { SchemaWithPipe } from '../../methods/index.ts';
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
import type { NullishSchema } from '../nullish/index.ts';
Expand Down Expand Up @@ -47,6 +47,12 @@ describe('objectAsync', () => {
key4: ObjectSchemaAsync<{ key: NumberSchema<undefined> }, never>;
key5: SchemaWithPipe<[StringSchema<undefined>, ReadonlyAction<string>]>;
key6: UndefinedableSchema<StringSchema<undefined>, 'bar'>;
key7: SchemaWithPipe<
[
OptionalSchema<StringSchema<undefined>, never>,
TransformAction<undefined | string, string>,
]
>;
},
undefined
>;
Expand All @@ -59,6 +65,7 @@ describe('objectAsync', () => {
key4: { key: number };
key5: string;
key6: string | undefined;
key7?: string;
}>();
});

Expand All @@ -70,6 +77,7 @@ describe('objectAsync', () => {
key4: { key: number };
readonly key5: string;
key6: string;
key7: string;
}>();
});

Expand Down
10 changes: 9 additions & 1 deletion library/src/schemas/objectWithRest/objectWithRest.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { ReadonlyAction } from '../../actions/index.ts';
import type { ReadonlyAction, TransformAction } from '../../actions/index.ts';
import type { SchemaWithPipe } from '../../methods/index.ts';
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
import type { BooleanIssue, BooleanSchema } from '../boolean/index.ts';
Expand Down Expand Up @@ -57,6 +57,12 @@ describe('objectWithRest', () => {
key4: ObjectSchema<{ key: NumberSchema<undefined> }, never>;
key5: SchemaWithPipe<[StringSchema<undefined>, ReadonlyAction<string>]>;
key6: UndefinedableSchema<StringSchema<undefined>, 'bar'>;
key7: SchemaWithPipe<
[
OptionalSchema<StringSchema<undefined>, never>,
TransformAction<undefined | string, string>,
]
>;
},
BooleanSchema<undefined>,
undefined
Expand All @@ -71,6 +77,7 @@ describe('objectWithRest', () => {
key4: { key: number };
key5: string;
key6: string | undefined;
key7?: string;
} & { [key: string]: boolean }
>();
});
Expand All @@ -84,6 +91,7 @@ describe('objectWithRest', () => {
key4: { key: number };
readonly key5: string;
key6: string;
key7: string;
} & { [key: string]: boolean }
>();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { ReadonlyAction } from '../../actions/index.ts';
import type { ReadonlyAction, TransformAction } from '../../actions/index.ts';
import type { SchemaWithPipe } from '../../methods/index.ts';
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
import type { BooleanIssue, BooleanSchema } from '../boolean/index.ts';
Expand Down Expand Up @@ -60,6 +60,12 @@ describe('objectWithRestAsync', () => {
key4: ObjectSchema<{ key: NumberSchema<undefined> }, never>;
key5: SchemaWithPipe<[StringSchema<undefined>, ReadonlyAction<string>]>;
key6: UndefinedableSchema<StringSchema<undefined>, 'bar'>;
key7: SchemaWithPipe<
[
OptionalSchema<StringSchema<undefined>, never>,
TransformAction<undefined | string, string>,
]
>;
},
BooleanSchema<undefined>,
undefined
Expand All @@ -74,6 +80,7 @@ describe('objectWithRestAsync', () => {
key4: { key: number };
key5: string;
key6: string | undefined;
key7?: string;
} & { [key: string]: boolean }
>();
});
Expand All @@ -87,6 +94,7 @@ describe('objectWithRestAsync', () => {
key4: { key: number };
readonly key5: string;
key6: string;
key7: string;
} & { [key: string]: boolean }
>();
});
Expand Down
10 changes: 9 additions & 1 deletion library/src/schemas/strictObject/strictObject.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { ReadonlyAction } from '../../actions/index.ts';
import type { ReadonlyAction, TransformAction } from '../../actions/index.ts';
import type { SchemaWithPipe } from '../../methods/index.ts';
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
import type { NullishSchema } from '../nullish/index.ts';
Expand Down Expand Up @@ -48,6 +48,12 @@ describe('strictObject', () => {
key4: ObjectSchema<{ key: NumberSchema<undefined> }, never>;
key5: SchemaWithPipe<[StringSchema<undefined>, ReadonlyAction<string>]>;
key6: UndefinedableSchema<StringSchema<undefined>, 'bar'>;
key7: SchemaWithPipe<
[
OptionalSchema<StringSchema<undefined>, never>,
TransformAction<undefined | string, string>,
]
>;
},
undefined
>;
Expand All @@ -60,6 +66,7 @@ describe('strictObject', () => {
key4: { key: number };
key5: string;
key6: string | undefined;
key7?: string;
}>();
});

Expand All @@ -71,6 +78,7 @@ describe('strictObject', () => {
key4: { key: number };
readonly key5: string;
key6: string;
key7: string;
}>();
});

Expand Down
10 changes: 9 additions & 1 deletion library/src/schemas/strictObject/strictObjectAsync.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { ReadonlyAction } from '../../actions/index.ts';
import type { ReadonlyAction, TransformAction } from '../../actions/index.ts';
import type { SchemaWithPipe } from '../../methods/index.ts';
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
import type { NullishSchema } from '../nullish/index.ts';
Expand Down Expand Up @@ -53,6 +53,12 @@ describe('strictObjectAsync', () => {
key4: ObjectSchema<{ key: NumberSchema<undefined> }, never>;
key5: SchemaWithPipe<[StringSchema<undefined>, ReadonlyAction<string>]>;
key6: UndefinedableSchema<StringSchema<undefined>, 'bar'>;
key7: SchemaWithPipe<
[
OptionalSchema<StringSchema<undefined>, never>,
TransformAction<undefined | string, string>,
]
>;
},
undefined
>;
Expand All @@ -65,6 +71,7 @@ describe('strictObjectAsync', () => {
key4: { key: number };
key5: string;
key6: string | undefined;
key7?: string;
}>();
});

Expand All @@ -76,6 +83,7 @@ describe('strictObjectAsync', () => {
key4: { key: number };
readonly key5: string;
key6: string;
key7: string;
}>();
});

Expand Down
8 changes: 5 additions & 3 deletions library/src/types/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ type OptionalInputKeys<TEntries extends ObjectEntries | ObjectEntriesAsync> = {
*/
type OptionalOutputKeys<TEntries extends ObjectEntries | ObjectEntriesAsync> = {
[TKey in keyof TEntries]: TEntries[TKey] extends QuestionMarkSchema
? HasDefault<TEntries[TKey]> extends true
? never
: TKey
? undefined extends InferOutput<TEntries[TKey]>
? HasDefault<TEntries[TKey]> extends false
? TKey
: never
: never
: never;
}[keyof TEntries];

Expand Down

0 comments on commit fe4d1fd

Please sign in to comment.