Skip to content

Commit

Permalink
Allow parent resolve before describe. (#2051)
Browse files Browse the repository at this point in the history
* Allow parent conditions in describe

* Remove _describe method, use multiple resolves
  • Loading branch information
DaddyWarbucks committed Sep 29, 2023
1 parent 6956ee7 commit 020901f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ export default class ArraySchema<
}

describe(options?: ResolveOptions<TContext>) {
let base = super.describe(options) as SchemaInnerTypeDescription;
if (this.innerType) {
const next = (options ? this.resolve(options) : this).clone();
const base = super.describe(options) as SchemaInnerTypeDescription;
if (next.innerType) {
let innerOptions = options;
if (innerOptions?.value) {
innerOptions = {
Expand All @@ -279,7 +280,7 @@ export default class ArraySchema<
value: innerOptions.value[0],
};
}
base.innerType = this.innerType.describe(innerOptions);
base.innerType = next.innerType.describe(innerOptions);
}
return base;
}
Expand Down
5 changes: 3 additions & 2 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,10 @@ export default class ObjectSchema<
}

describe(options?: ResolveOptions<TContext>) {
let base = super.describe(options) as SchemaObjectDescription;
const next = (options ? this.resolve(options) : this).clone();
const base = super.describe(options) as SchemaObjectDescription;
base.fields = {};
for (const [key, value] of Object.entries(this.fields)) {
for (const [key, value] of Object.entries(next.fields)) {
let innerOptions = options;
if (innerOptions?.value) {
innerOptions = {
Expand Down
5 changes: 3 additions & 2 deletions src/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ export default class TupleSchema<
}

describe(options?: ResolveOptions<TContext>) {
let base = super.describe(options) as SchemaInnerTypeDescription;
base.innerType = this.spec.types.map((schema, index) => {
const next = (options ? this.resolve(options) : this).clone();
const base = super.describe(options) as SchemaInnerTypeDescription;
base.innerType = next.spec.types.map((schema, index) => {
let innerOptions = options;
if (innerOptions?.value) {
innerOptions = {
Expand Down
17 changes: 17 additions & 0 deletions test/mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,11 @@ describe('Mixed Types ', () => {
then: (s) => s.defined(),
}),
baz: tuple([string(), number()]),
})
.when(['dummy'], (_, s) => {
return s.shape({
when: string()
})
});
});

Expand Down Expand Up @@ -1087,6 +1092,7 @@ describe('Mixed Types ', () => {
bar: 'a',
lazy: undefined,
baz: undefined,
when: undefined,
},
nullable: false,
optional: true,
Expand Down Expand Up @@ -1185,6 +1191,17 @@ describe('Mixed Types ', () => {
},
],
},
when: {
type: 'string',
meta: undefined,
label: undefined,
default: undefined,
notOneOf: [],
nullable: false,
oneOf: [],
optional: true,
tests: [],
}
},
});
});
Expand Down

0 comments on commit 020901f

Please sign in to comment.