-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add json() method and remove default object/array coercion
BREAKING CHANGE: object and array schema no longer parse JSON strings by default, nor do they return `null` for invalid casts. ```ts object().json().cast('{}') array().json().cast('[]') ``` to mimic the previous behavior
- Loading branch information
Showing
6 changed files
with
54 additions
and
44 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,18 @@ | ||
import type { AnySchema } from '..'; | ||
import type { TransformFunction } from '../types'; | ||
|
||
const parseJson: TransformFunction<any> = (value, _, ctx: AnySchema<any>) => { | ||
if (typeof value !== 'string') { | ||
return value; | ||
} | ||
|
||
let parsed = value; | ||
try { | ||
parsed = JSON.parse(value); | ||
} catch (err) { | ||
/* */ | ||
} | ||
return ctx.isType(parsed) ? parsed : value; | ||
}; | ||
|
||
export default parseJson; |
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