-
-
Couldn't load subscription status.
- Fork 37
feat: add omitAdditionalProperties option
#220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
toomuchdesign
wants to merge
6
commits into
ThomasAribart:main
Choose a base branch
from
toomuchdesign:omitAdditionalProperties-option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
435e9ae
chore: fix format issue on package.json
toomuchdesign fe3213e
feat: add omitAdditionalProperties option type
toomuchdesign 9382ac5
test: add failing tests for omitAdditionalProperties option
toomuchdesign 3cb5697
feat: implement omitAdditionalProperties option
toomuchdesign d41d992
test: elisnt errors
toomuchdesign b88d314
refactor: GetClosedOnResolve generic
toomuchdesign File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import { expectTypeOf } from "expect-type"; | ||
|
|
||
| import type { FromSchema } from "~/index"; | ||
|
|
||
| import { ajv } from "./ajv.util.test"; | ||
|
|
@@ -45,6 +47,17 @@ describe("Object schemas", () => { | |
| setInstance = { a: 42 }; | ||
| expect(ajv.validate(setSchema, setInstance)).toBe(false); | ||
| }); | ||
|
|
||
| describe("with omitAdditionalProperties option", () => { | ||
| it("rejects additional properties", () => { | ||
| type Set2 = FromSchema< | ||
| typeof setSchema, | ||
| { omitAdditionalProperties: true } | ||
| >; | ||
|
|
||
| expectTypeOf<Set2>().toEqualTypeOf<{}>(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Pattern properties", () => { | ||
|
|
@@ -95,6 +108,17 @@ describe("Object schemas", () => { | |
| objInstance = { B: true, S: "str", I: 42, N: null }; | ||
| expect(ajv.validate(boolStrOrNumObjSchema, objInstance)).toBe(false); | ||
| }); | ||
|
|
||
| describe("with omitAdditionalProperties option", () => { | ||
| it("rejects object with boolean value", () => { | ||
| type Set = FromSchema< | ||
| typeof boolStrOrNumObjSchema, | ||
| { omitAdditionalProperties: true } | ||
| >; | ||
|
|
||
| expectTypeOf<Set>().toEqualTypeOf<{}>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: ended up using expect-type to assert against {} type. |
||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Unevaluated properties", () => { | ||
|
|
@@ -286,7 +310,7 @@ describe("Object schemas", () => { | |
| expect(ajv.validate(addressSchema, addressInstance)).toBe(true); | ||
| }); | ||
|
|
||
| it("accepts object with missing required properties", () => { | ||
| it("rejects object with missing required properties", () => { | ||
| // @ts-expect-error | ||
| addressInstance = { | ||
| number: 13, | ||
|
|
@@ -295,6 +319,25 @@ describe("Object schemas", () => { | |
| }; | ||
| expect(ajv.validate(addressSchema, addressInstance)).toBe(false); | ||
| }); | ||
|
|
||
| describe("with omitAdditionalProperties option", () => { | ||
| it("rejects object with additional properties", () => { | ||
| type Address2 = FromSchema< | ||
| typeof addressSchema, | ||
| { omitAdditionalProperties: true } | ||
| >; | ||
|
|
||
| const addressInstance2: Address2 = { | ||
| number: 13, | ||
| streetName: "Champs Elysées", | ||
| streetType: "Avenue", | ||
| direction: "NW", | ||
| // @ts-expect-error | ||
| additionalProperty: ["any", "value"], | ||
| }; | ||
| expect(ajv.validate(addressSchema, addressInstance2)).toBe(true); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Required + Typed additional properties", () => { | ||
|
|
@@ -345,6 +388,24 @@ describe("Object schemas", () => { | |
| }; | ||
| expect(ajv.validate(addressSchema, addressInstance)).toBe(false); | ||
| }); | ||
|
|
||
| describe("with omitAdditionalProperties option", () => { | ||
| it("rejects object with valid additional properties", () => { | ||
| type Address2 = FromSchema< | ||
| typeof addressSchema, | ||
| { omitAdditionalProperties: true } | ||
| >; | ||
|
|
||
| const addressInstance2: Address2 = { | ||
| number: 13, | ||
| streetName: "Champs Elysées", | ||
| streetType: "Avenue", | ||
| // @ts-expect-error | ||
| additionalProperty: "additionalProperty", | ||
| }; | ||
| expect(ajv.validate(addressSchema, addressInstance2)).toBe(true); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Required missing in properties", () => { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ended up using expect-type to assert against {} type.
Happy to find an alternative solution if we prefer not to introduce expect-type.
If we find expect-type valuable, we could refactor tests to use it extensively.