Skip to content

Commit

Permalink
fix(shape): Reset schema state.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jan 26, 2020
1 parent 3c3bd12 commit 5f57bc0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
@@ -1,4 +1,10 @@
### 4.1.1 - 2020-01-29
### 4.1.2 - 2020-01-26

#### 🐞 Fixes

- **[shape]** Reset schema state after validating children.

### 4.1.1 - 2020-01-24

#### 🐞 Fixes

Expand Down
6 changes: 6 additions & 0 deletions src/predicates/Shape.ts
Expand Up @@ -48,6 +48,8 @@ export default class ShapePredicate<T extends object> extends Predicate<T> {

const unknownFields: Partial<T> = { ...value };
const struct: Partial<T> = { ...value };
const oldPath = this.schema?.parentPath;
const oldStruct = this.schema?.parentStruct;

this.schema!.parentPath = path;
this.schema!.parentStruct = struct;
Expand All @@ -71,6 +73,10 @@ export default class ShapePredicate<T extends object> extends Predicate<T> {
Object.assign(struct, unknownFields);
}

// Reset state for next check
this.schema!.parentPath = oldPath!;
this.schema!.parentStruct = oldStruct!;

return struct as T;
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/helpers.ts
Expand Up @@ -6,12 +6,12 @@ export function runChecks<T>(
value?: Partial<T> | null,
{ key = 'key', struct }: { key?: string; struct?: object } = {},
): T | null {
const schema = new Schema({});
const schema = predicate.schema || new Schema({});
const currentStruct = struct ?? { [key]: value };

schema.struct = currentStruct;
schema.parentStruct = currentStruct;
schema.initialStruct = currentStruct;
schema.struct = { ...currentStruct };
schema.parentStruct = { ...currentStruct };
schema.initialStruct = { ...currentStruct };

predicate.schema = schema;

Expand Down

0 comments on commit 5f57bc0

Please sign in to comment.