Skip to content

Commit

Permalink
feat: improve zod types (#4173)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a authored Sep 12, 2023
1 parent effad1b commit a315f9b
Showing 1 changed file with 23 additions and 42 deletions.
65 changes: 23 additions & 42 deletions packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,20 +337,22 @@ export type Resolve = z.infer<typeof resolve>;
//#endregion

//#region Module
export type RuleSetCondition =
| RegExp
| string
| RuleSetConditions
| RuleSetLogicalConditions
| ((value: string) => boolean);
const ruleSetCondition: z.ZodType<RuleSetCondition> = z
const baseRuleSetCondition = z
.instanceof(RegExp)
.or(z.string())
.or(z.lazy(() => ruleSetConditions))
.or(z.lazy(() => ruleSetLogicalConditions))
.or(z.function().args(z.string()).returns(z.boolean()));

export type RuleSetCondition =
| z.infer<typeof baseRuleSetCondition>
| RuleSetConditions
| RuleSetLogicalConditions;

const ruleSetCondition: z.ZodType<RuleSetCondition> = baseRuleSetCondition
.or(z.lazy(() => ruleSetConditions))
.or(z.lazy(() => ruleSetLogicalConditions));

export type RuleSetConditions = RuleSetCondition[];

const ruleSetConditions: z.ZodType<RuleSetConditions> = z.lazy(() =>
z.array(ruleSetCondition)
);
Expand All @@ -360,6 +362,7 @@ export type RuleSetLogicalConditions = {
or?: RuleSetConditions;
not?: RuleSetConditions;
};

const ruleSetLogicalConditions: z.ZodType<RuleSetLogicalConditions> =
z.strictObject({
and: ruleSetConditions.optional(),
Expand Down Expand Up @@ -390,37 +393,7 @@ const ruleSetUse = ruleSetUseItem
);
export type RuleSetUse = z.infer<typeof ruleSetUse>;

export type RuleSetRule = {
test?: RuleSetCondition;
exclude?: RuleSetCondition;
include?: RuleSetCondition;
issuer?: RuleSetCondition;
dependency?: RuleSetCondition;
resource?: RuleSetCondition;
resourceFragment?: RuleSetCondition;
resourceQuery?: RuleSetCondition;
scheme?: RuleSetCondition;
mimetype?: RuleSetCondition;
descriptionData?: {
[k: string]: RuleSetCondition;
};
oneOf?: RuleSetRule[];
rules?: RuleSetRule[];
type?: string;
loader?: RuleSetLoader;
options?: RuleSetLoaderOptions;
use?: RuleSetUse;
parser?: {
[k: string]: any;
};
generator?: {
[k: string]: any;
};
resolve?: ResolveOptions;
sideEffects?: boolean;
enforce?: "pre" | "post";
};
const ruleSetRule: z.ZodType<RuleSetRule> = z.strictObject({
const baseRuleSetRule = z.strictObject({
test: ruleSetCondition.optional(),
exclude: ruleSetCondition.optional(),
include: ruleSetCondition.optional(),
Expand All @@ -432,8 +405,6 @@ const ruleSetRule: z.ZodType<RuleSetRule> = z.strictObject({
scheme: ruleSetCondition.optional(),
mimetype: ruleSetCondition.optional(),
descriptionData: z.record(ruleSetCondition).optional(),
oneOf: z.lazy(() => ruleSetRule.array()).optional(),
rules: z.lazy(() => ruleSetRule.array()).optional(),
type: z.string().optional(),
loader: ruleSetLoader.optional(),
options: ruleSetLoaderOptions.optional(),
Expand All @@ -445,6 +416,16 @@ const ruleSetRule: z.ZodType<RuleSetRule> = z.strictObject({
enforce: z.literal("pre").or(z.literal("post")).optional()
});

export type RuleSetRule = z.infer<typeof baseRuleSetRule> & {
oneOf?: RuleSetRule[];
rules?: RuleSetRule[];
};

const ruleSetRule: z.ZodType<RuleSetRule> = baseRuleSetRule.extend({
oneOf: z.lazy(() => ruleSetRule.array()).optional(),
rules: z.lazy(() => ruleSetRule.array()).optional()
});

const ruleSetRules = z.array(z.literal("...").or(ruleSetRule));
export type RuleSetRules = z.infer<typeof ruleSetRules>;

Expand Down

0 comments on commit a315f9b

Please sign in to comment.