Skip to content

Commit

Permalink
Replace old struct
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Oct 21, 2024
1 parent 9b35af5 commit 43a2f02
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 34 deletions.
1 change: 0 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe('index', () => {
"KnownCaipNamespace",
"PendingJsonRpcResponseStruct",
"StrictHexStruct",
"UnsafeFastJsonStruct",
"UnsafeJsonStruct",
"VersionRangeStruct",
"VersionStruct",
Expand Down
35 changes: 3 additions & 32 deletions src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,6 @@ export function exactOptional<Type, Schema>(
});
}

/**
* A struct to check if the given value is finite number. Superstruct's
* `number()` struct does not check if the value is finite.
*
* @returns A struct to check if the given value is finite number.
*/
const finiteNumber = () =>
define<number>('finite number', (value) => {
return is(value, number()) && Number.isFinite(value);
});

/**
* Validate an unknown input to be valid JSON.
*
Expand Down Expand Up @@ -216,36 +205,18 @@ function validateJson(json: unknown): boolean {
*
* Note that this struct is unsafe. For safe validation, use {@link JsonStruct}.
*/
export const UnsafeFastJsonStruct: Struct<Json> = define('JSON', (json) =>
export const UnsafeJsonStruct: Struct<Json> = define('JSON', (json) =>
validateJson(json),
);

/**
* A struct to check if the given value is a valid JSON-serializable value.
*
* Note that this struct is unsafe. For safe validation, use {@link JsonStruct}.
*/
// We cannot infer the type of the struct, because it is recursive.
export const UnsafeJsonStruct: Struct<Json> = union([
literal(null),
boolean(),
finiteNumber(),
string(),
array(lazy(() => UnsafeJsonStruct)),
record(
string(),
lazy(() => UnsafeJsonStruct),
),
]);

/**
* A struct to check if the given value is a valid JSON-serializable value.
*
* This struct sanitizes the value before validating it, so that it is safe to
* use with untrusted input.
*/
export const JsonStruct = coerce(UnsafeFastJsonStruct, any(), (value) => {
assertStruct(value, UnsafeFastJsonStruct);
export const JsonStruct = coerce(UnsafeJsonStruct, any(), (value) => {
assertStruct(value, UnsafeJsonStruct);
return JSON.parse(
JSON.stringify(value, (propKey, propValue) => {
// Strip __proto__ and constructor properties to prevent prototype pollution.
Expand Down
1 change: 0 additions & 1 deletion src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe('node', () => {
"KnownCaipNamespace",
"PendingJsonRpcResponseStruct",
"StrictHexStruct",
"UnsafeFastJsonStruct",
"UnsafeJsonStruct",
"VersionRangeStruct",
"VersionStruct",
Expand Down

0 comments on commit 43a2f02

Please sign in to comment.