-
Notifications
You must be signed in to change notification settings - Fork 4
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 #37 from Prefinem/parse
Transform types 'post' and 'pre'
- Loading branch information
Showing
8 changed files
with
313 additions
and
210 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
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,12 @@ | ||
const transformValue = require('./transformValue'); | ||
|
||
const transformRecord = (schema, record) => | ||
Object.keys(record).reduce( | ||
(result, key) => ({ | ||
...result, | ||
[key]: schema[key] ? transformValue(schema[key], record[key], 'pre') : record[key], | ||
}), | ||
{}, | ||
); | ||
|
||
module.exports = transformRecord; |
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,4 @@ | ||
const transformValue = (type, value, key) => | ||
type.transforms && type.transforms[key] ? type.transforms[key](value) : value; | ||
|
||
module.exports = transformValue; |
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,23 @@ | ||
const { | ||
createModel, | ||
customtypes: { email }, | ||
datatypes: { string }, | ||
} = require('./../src'); | ||
|
||
test('transform - pre', async () => { | ||
const schema = { | ||
foo: email().transform((value) => (value === '' ? undefined : value), 'pre'), | ||
}; | ||
const model = createModel(schema); | ||
|
||
await expect(model({ foo: '' })).toEqual({}); | ||
}); | ||
|
||
test('transform - post', async () => { | ||
const schema = { | ||
foo: string().transform((value) => (value === '' ? 'bar' : value), 'post'), | ||
}; | ||
const model = createModel(schema); | ||
|
||
await expect(model({ foo: '' })).toEqual({ foo: 'bar' }); | ||
}); |
Oops, something went wrong.