Skip to content

Commit

Permalink
Merge pull request #39 from alexhutsau/master
Browse files Browse the repository at this point in the history
Use Nested options validator as priority validator
  • Loading branch information
AmauryD authored Feb 23, 2024
2 parents c144c5d + 9b72c02 commit 965df37
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type NestedOptions = Partial<RuleCustom> & { validator?: Class<any> };

export function Nested (options: NestedOptions = {}): any {
return (target: Class<any>, key: string): any => {
const t = Reflect.getMetadata("design:type", target, key) ?? options.validator;
const t = options.validator ?? Reflect.getMetadata("design:type", target, key);
if (!t) {
throw new Error(`Cannot get \"design:type\" of ${key}. Please use the validator option to @Nested`);
}
Expand Down
27 changes: 26 additions & 1 deletion tests/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,31 @@ describe("Nested", () => {
});
});

it("Should apply nullable nested schema", () => {
@Schema({ strict: true })
class NestedTest {
@Boolean()
prop!: boolean;
}
@Schema()
class Test {
@Nested({
validator: NestedTest
})
prop?: NestedTest | null;
}
expect(getSchema(Test)).toEqual({
$$strict: false,
prop: {
type: "object",
strict: true,
props: {
prop: { type: "boolean" },
},
},
});
});

it("Should not remove nested $$strict", () => {
@Schema()
class NestedTest {
Expand Down Expand Up @@ -963,4 +988,4 @@ describe("Nested Array", () => {
});
expect(validationResult).toStrictEqual(true);
});
});
});

0 comments on commit 965df37

Please sign in to comment.