From cad31f022ed71c1188822ecea5c4610aceb64a54 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Sat, 11 May 2024 14:07:04 +0400 Subject: [PATCH] Removed s.failWithError in favor of S.Error.raise --- CHANGELOG_V7.md | 1 + IDEAS.md | 1 - docs/rescript-usage.md | 12 +++--------- packages/tests/src/core/S_custom_test.res | 13 +++---------- packages/tests/src/core/S_failWithError_test.res | 14 +++++++------- packages/tests/src/core/S_transform_test.res | 14 +++++++------- src/S.resi | 2 -- src/S_Core.bs.mjs | 6 ------ src/S_Core.res | 12 ------------ src/S_Core.resi | 2 -- 10 files changed, 21 insertions(+), 56 deletions(-) diff --git a/CHANGELOG_V7.md b/CHANGELOG_V7.md index 279326ec..36075b07 100644 --- a/CHANGELOG_V7.md +++ b/CHANGELOG_V7.md @@ -16,3 +16,4 @@ - More flexible - Improved tree-shaking - Can get the info from the `tagged` type: `JSON` -> `JSON({validated: bool})` +- Removed `s.failWithError`. Use `S.Error.raise` instead diff --git a/IDEAS.md b/IDEAS.md index 7ffff4dd..995dd638 100644 --- a/IDEAS.md +++ b/IDEAS.md @@ -29,7 +29,6 @@ let trimContract: S.contract string> = S.contract(s => { - stop reallocate objects without transformations - Make S.serializeToJsonString super fast - Make operations more treeshakable by starting passing the actual operation to the initialOperation function. Or add a condition (verify performance) -- Remove `s.failWithError` since there's `Error.raise` 🤔 - Turn `String.email` -> `email`, `String.min` -> `stringMin` for tree-shaking - Rename `InvalidJsonStruct` error, since after `rescript-struct`->`rescript-schema` it became misleading - Add S.bigint diff --git a/docs/rescript-usage.md b/docs/rescript-usage.md index d5c626dc..da076f33 100644 --- a/docs/rescript-usage.md +++ b/docs/rescript-usage.md @@ -892,24 +892,18 @@ You can also define your own custom schema factories that are specific to your a ```rescript let nullableSchema = innerSchema => { - S.custom("Nullable", s => { + S.custom("Nullable", _ => { parser: unknown => { if unknown === %raw(`undefined`) || unknown === %raw(`null`) { None } else { - switch unknown->S.parseAnyWith(innerSchema) { - | Ok(value) => Some(value) - | Error(error) => s.failWithError(error) - } + Some(unknown->S.parseAnyOrRaiseWith(innerSchema)) } }, serializer: value => { switch value { | Some(innerValue) => - switch innerValue->S.serializeToUnknownWith(innerSchema) { - | Ok(value) => value - | Error(error) => s.failWithError(error) - } + innerValue->S.serializeToUnknownOrRaiseWith(innerSchema) | None => %raw(`null`) } }, diff --git a/packages/tests/src/core/S_custom_test.res b/packages/tests/src/core/S_custom_test.res index 09ee6f8a..592340b3 100644 --- a/packages/tests/src/core/S_custom_test.res +++ b/packages/tests/src/core/S_custom_test.res @@ -2,24 +2,17 @@ open Ava open RescriptCore let nullableSchema = innerSchema => { - S.custom("Nullable", s => { + S.custom("Nullable", _ => { parser: unknown => { if unknown === %raw(`undefined`) || unknown === %raw(`null`) { None } else { - switch unknown->S.parseAnyWith(innerSchema) { - | Ok(value) => Some(value) - | Error(error) => s.failWithError(error) - } + Some(unknown->S.parseAnyOrRaiseWith(innerSchema)) } }, serializer: value => { switch value { - | Some(innerValue) => - switch innerValue->S.serializeToUnknownWith(innerSchema) { - | Ok(value) => value - | Error(error) => s.failWithError(error) - } + | Some(innerValue) => innerValue->S.serializeToUnknownOrRaiseWith(innerSchema) | None => %raw(`null`) } }, diff --git a/packages/tests/src/core/S_failWithError_test.res b/packages/tests/src/core/S_failWithError_test.res index b3bb195d..c91a69fa 100644 --- a/packages/tests/src/core/S_failWithError_test.res +++ b/packages/tests/src/core/S_failWithError_test.res @@ -1,10 +1,10 @@ open Ava -test("FIXME: Should keep operation of the error passed to advanced fail", t => { +test("Keeps operation of the error passed to S.Error.raise", t => { let schema = S.array( - S.string->S.transform(s => { + S.string->S.transform(_ => { parser: _ => - s.failWithError( + S.Error.raise( U.error({ code: OperationFailed("User error"), operation: Serializing, @@ -18,7 +18,7 @@ test("FIXME: Should keep operation of the error passed to advanced fail", t => { ["Hello world!"]->S.parseAnyWith(schema), { code: OperationFailed("User error"), - operation: Parsing, + operation: Serializing, path: S.Path.fromArray(["0", "a", "b"]), }, ) @@ -30,11 +30,11 @@ test("Works with failing outside of the parser", t => { "root", S.array( S.string->S.transform( - s => - s.failWithError( + _ => + S.Error.raise( U.error({ code: OperationFailed("User error"), - operation: Serializing, + operation: Parsing, path: S.Path.fromArray(["a", "b"]), }), ), diff --git a/packages/tests/src/core/S_transform_test.res b/packages/tests/src/core/S_transform_test.res index 1bea5a29..a4661d57 100644 --- a/packages/tests/src/core/S_transform_test.res +++ b/packages/tests/src/core/S_transform_test.res @@ -47,11 +47,11 @@ test("Fails to parse when user raises error in a Transformed Primitive parser", ) }) -test("Uses the path from failWithError called in the transform parser", t => { +test("Uses the path from S.Error.raise called in the transform parser", t => { let schema = S.array( - S.string->S.transform(s => { + S.string->S.transform(_ => { parser: _ => - s.failWithError( + S.Error.raise( U.error({ code: OperationFailed("User error"), operation: Parsing, @@ -71,14 +71,14 @@ test("Uses the path from failWithError called in the transform parser", t => { ) }) -test("Uses the path from failWithError called in the transform serializer", t => { +test("Uses the path from S.Error.raise called in the transform serializer", t => { let schema = S.array( - S.string->S.transform(s => { + S.string->S.transform(_ => { serializer: _ => - s.failWithError( + S.Error.raise( U.error({ code: OperationFailed("User error"), - operation: Parsing, + operation: Serializing, path: S.Path.fromArray(["a", "b"]), }), ), diff --git a/src/S.resi b/src/S.resi index 77ffe73d..1b86f3f4 100644 --- a/src/S.resi +++ b/src/S.resi @@ -73,7 +73,6 @@ type exn += private Raised(error) type s<'value> = { schema: t<'value>, fail: 'a. (string, ~path: Path.t=?) => 'a, - failWithError: 'a. error => 'a, } module Error: { @@ -126,7 +125,6 @@ module Catch: { @as("i") input: unknown, @as("s") schema: t<'value>, @as("f") fail: 'a. (string, ~path: Path.t=?) => 'a, - @as("w") failWithError: 'a. error => 'a, } } let catch: (t<'value>, Catch.s<'value> => 'value) => t<'value> diff --git a/src/S_Core.bs.mjs b/src/S_Core.bs.mjs index bd22773e..f6f7c6d1 100644 --- a/src/S_Core.bs.mjs +++ b/src/S_Core.bs.mjs @@ -95,9 +95,6 @@ function make(selfSchema, path, operation) { TAG: "OperationFailed", _0: message }, operation, path + customPath); - }), - failWithError: (function (error) { - throw new RescriptSchemaError(error.code, operation, path + error.path); }) }; } @@ -2584,9 +2581,6 @@ function $$catch(schema, getFallbackValue) { TAG: "OperationFailed", _0: message }, b.o, path + customPath); - }), - w: (function (error) { - throw new RescriptSchemaError(error.code, b.o, path + error.path); }) }); }) - 1) + "](" + inputVar + "," + errorVar + ")"; diff --git a/src/S_Core.res b/src/S_Core.res index d72b6529..094f8470 100644 --- a/src/S_Core.res +++ b/src/S_Core.res @@ -364,15 +364,11 @@ module InternalError = { type s<'value> = { schema: t<'value>, fail: 'a. (string, ~path: Path.t=?) => 'a, - failWithError: 'a. error => 'a, } module EffectCtx = { let make = (~selfSchema, ~path, ~operation) => { schema: selfSchema->castUnknownSchemaToAnySchema, - failWithError: (error: error) => { - InternalError.raise(~path=path->Path.concat(error.path), ~code=error.code, ~operation) - }, fail: (message, ~path as customPath=Path.empty) => { InternalError.raise( ~path=path->Path.concat(customPath), @@ -3346,7 +3342,6 @@ module Catch = { @as("i") input: unknown, @as("s") schema: t<'value>, @as("f") fail: 'a. (string, ~path: Path.t=?) => 'a, - @as("w") failWithError: 'a. error => 'a, } } let catch = (schema, getFallbackValue) => { @@ -3362,13 +3357,6 @@ let catch = (schema, getFallbackValue) => { Catch.input, error: internalError, schema: selfSchema->castUnknownSchemaToAnySchema, - failWithError: (error: error) => { - InternalError.raise( - ~path=path->Path.concat(error.path), - ~code=error.code, - ~operation=b.operation, - ) - }, fail: (message, ~path as customPath=Path.empty) => { InternalError.raise( ~path=path->Path.concat(customPath), diff --git a/src/S_Core.resi b/src/S_Core.resi index 79291972..dbfbfc48 100644 --- a/src/S_Core.resi +++ b/src/S_Core.resi @@ -73,7 +73,6 @@ type exn += private Raised(error) type s<'value> = { schema: t<'value>, fail: 'a. (string, ~path: Path.t=?) => 'a, - failWithError: 'a. error => 'a, } module Error: { @@ -126,7 +125,6 @@ module Catch: { @as("i") input: unknown, @as("s") schema: t<'value>, @as("f") fail: 'a. (string, ~path: Path.t=?) => 'a, - @as("w") failWithError: 'a. error => 'a, } } let catch: (t<'value>, Catch.s<'value> => 'value) => t<'value>