-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow setting
typeDefault
on nopt
and clean
lib methods
Setting this option allows bypassing the default handling of unknown keys when cleaning data.
- Loading branch information
1 parent
15befd2
commit 7ede6c7
Showing
4 changed files
with
75 additions
and
16 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,48 @@ | ||
const t = require('tap') | ||
const nopt = require('../lib/nopt-lib.js') | ||
|
||
t.test('use other type default', (t) => { | ||
const NotAllowed = Symbol('NotAllowed') | ||
const Invalid = Symbol('Invalid') | ||
|
||
const clean = (data, opts) => { | ||
const invalids = [] | ||
nopt.clean(data, { | ||
types: { | ||
str: nopt.typeDefs.String.type, | ||
invalid: Invalid, | ||
}, | ||
typeDefs: { | ||
...nopt.typeDefs, | ||
NotAllowed: { type: NotAllowed, validate: () => false }, | ||
Invalid: { type: Invalid, validate: () => false }, | ||
}, | ||
invalidHandler: (k, v, type) => invalids.push([k, v, type]), | ||
...opts, | ||
}) | ||
return { | ||
keys: Object.keys(data), | ||
invalids, | ||
} | ||
} | ||
|
||
t.strictSame(clean({ | ||
str: 'aaa', | ||
invalid: 'bad', | ||
unknown: 'huh?', | ||
}), { | ||
keys: ['str', 'unknown'], | ||
invalids: [['invalid', 'bad', Invalid]], | ||
}, 'invalid data is removed with clean') | ||
|
||
t.strictSame(clean({ | ||
str: 'aaa', | ||
invalid: 'bad', | ||
unknown: 'huh?', | ||
}, { typeDefault: NotAllowed }), { | ||
keys: ['str'], | ||
invalids: [['invalid', 'bad', Invalid], ['unknown', 'huh?', NotAllowed]], | ||
}, 'invalid and unknown data is removed with a custom typeDefault') | ||
|
||
t.end() | ||
}) |