-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from remnantkevin/add-non-strict-modifier
Add `nonStrict` modifier for `record` runtypes
- Loading branch information
Showing
12 changed files
with
338 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { internalRecord } from './record' | ||
import { Runtype, RuntypeUsageError } from './runtype' | ||
|
||
/** | ||
* Build a non-strict `record` runtype from the provided `record` runtype. | ||
* | ||
* In contrast to a `record` runtype, a non-strict `record` runtype will ignore | ||
* keys that are not specified in the original runtype's typemap (non-strict checking). | ||
* | ||
* When a non-strict `record` runtype checks an object, it will return a new | ||
* object that contains only the keys specified in the original runtype's typemap. | ||
* | ||
* Non-strict checking only applies to the root typemap. To apply non-strict checking | ||
* to a nested typemap, `nonStrict` needs to be used at each level of the typemap. | ||
*/ | ||
export function nonStrict<T>(original: Runtype<T>): Runtype<T> { | ||
const fields = (original as any).fields | ||
|
||
if (!fields) { | ||
throw new RuntypeUsageError('expected a record runtype') | ||
} | ||
|
||
return internalRecord(fields, true) | ||
} |
This file contains 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 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 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
Oops, something went wrong.