Skip to content

Commit

Permalink
Pass variant options as issue requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Nov 30, 2023
1 parent 3b03518 commit 7069548
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 25 deletions.
4 changes: 2 additions & 2 deletions library/src/schemas/variant/variant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('variant', () => {
input: { type: 'c', val: false },
},
],
requirement: ['a', 'b'],
requirement: schema.options,
},
]);

Expand All @@ -110,7 +110,7 @@ describe('variant', () => {
input: undefined,
origin: 'value',
path: [{ type: 'object', key: 'type', value: undefined, input: {} }],
requirement: ['a', 'b'],
requirement: schema.options,
},
]);
});
Expand Down
10 changes: 2 additions & 8 deletions library/src/schemas/variant/variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import type {
Output,
} from '../../types/index.ts';
import { getSchemaIssues, getOutput, getIssues } from '../../utils/index.ts';
import type { Literal } from '../literal/index.ts';
import type { LiteralSchema } from '../literal/literal.ts';
import type { ObjectPathItem, ObjectSchema } from '../object/index.ts';

/**
* Variant option type.
*/
export type VariantOption<TKey extends string> =
| ObjectSchema<Record<TKey, LiteralSchema<Literal>>, any>
| ObjectSchema<Record<TKey, BaseSchema>, any>
| (BaseSchema & {
type: 'variant';
options: VariantOptions<TKey>;
Expand Down Expand Up @@ -87,17 +85,13 @@ export function variant<
// Create issues and output
let issues: Issues | undefined;
let output: [Record<string, any>] | undefined;
const requirement: Literal[] = [];

// Create function to parse options recursively
const parseOptions = (options: VariantOptions<TKey>) => {
for (const schema of options) {
// If it is an object schema, parse discriminator key
if (schema.type === 'object') {
const variantKeySchema = schema.entries[this.key];

requirement.push(variantKeySchema.literal);

const result = variantKeySchema._parse(
(input as Record<TKey, unknown>)[this.key],
info
Expand Down Expand Up @@ -163,7 +157,7 @@ export function variant<
'Invalid variant key',
inputRecord[this.key],
undefined,
requirement
this.options
);
nonMatchingKeyIssues.issues[0].path = [pathItem];
return nonMatchingKeyIssues;
Expand Down
4 changes: 2 additions & 2 deletions library/src/schemas/variant/variantAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('variantAsync', () => {
input: { type: 'c', val: false },
},
],
requirement: ['a', 'b'],
requirement: schema.options,
},
]);

Expand All @@ -118,7 +118,7 @@ describe('variantAsync', () => {
input: undefined,
origin: 'value',
path: [{ type: 'object', key: 'type', value: undefined, input: {} }],
requirement: ['a', 'b'],
requirement: schema.options,
},
]);
});
Expand Down
16 changes: 3 additions & 13 deletions library/src/schemas/variant/variantAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import type {
Output,
} from '../../types/index.ts';
import { getSchemaIssues, getOutput, getIssues } from '../../utils/index.ts';
import type { LiteralSchema } from '../literal/literal.ts';
import type { LiteralSchemaAsync } from '../literal/literalAsync.ts';
import type { Literal } from '../literal/types.ts';
import type {
ObjectPathItem,
ObjectSchema,
Expand All @@ -20,11 +17,8 @@ import type {
* Variant option async type.
*/
export type VariantOptionAsync<TKey extends string> =
| ObjectSchema<Record<TKey, LiteralSchema<Literal>>, any>
| ObjectSchemaAsync<
Record<TKey, LiteralSchema<Literal> | LiteralSchemaAsync<Literal>>,
any
>
| ObjectSchema<Record<TKey, BaseSchema>, any>
| ObjectSchemaAsync<Record<TKey, BaseSchema | BaseSchemaAsync>, any>
| ((BaseSchema | BaseSchemaAsync) & {
type: 'variant';
options: VariantOptionsAsync<TKey>;
Expand Down Expand Up @@ -97,17 +91,13 @@ export function variantAsync<
// Create issues and output
let issues: Issues | undefined;
let output: [Record<string, any>] | undefined;
const requirement: Literal[] = [];

// Create function to parse options recursively
const parseOptions = async (options: VariantOptionsAsync<TKey>) => {
for (const schema of options) {
// If it is an object schema, parse discriminator key
if (schema.type === 'object') {
const variantKeySchema = schema.entries[this.key];

requirement.push(variantKeySchema.literal);

const result = await variantKeySchema._parse(
(input as Record<TKey, unknown>)[this.key],
info
Expand Down Expand Up @@ -173,7 +163,7 @@ export function variantAsync<
'Invalid variant key',
inputRecord[this.key],
undefined,
requirement
this.options
);
nonMatchingKeyIssues.issues[0].path = [pathItem];
return nonMatchingKeyIssues;
Expand Down

0 comments on commit 7069548

Please sign in to comment.