Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@effect/schema

Package Overview
Dependencies
Maintainers
3
Versions
335
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@effect/schema - npm Package Compare versions

Comparing version 0.3.1 to 0.4.0

15

Arbitrary.js

@@ -48,3 +48,3 @@ "use strict";

const getHook = /*#__PURE__*/AST.getAnnotation(ArbitraryHookId);
const go = /*#__PURE__*/I.memoize(ast => {
const go = ast => {
switch (ast._tag) {

@@ -177,3 +177,3 @@ case "Declaration":

const get = I.memoize(f);
return fc => fc.constant(null).chain(() => get(f)(fc));
return fc => fc.constant(null).chain(() => get()(fc));
}, handler => handler()));

@@ -191,6 +191,13 @@ case "Enums":

const to = go(ast.to);
return (0, _Function.pipe)(getHook(ast), O.match(() => fc => to(fc).filter(a => E.isRight((0, _ParseResult.eitherOrRunSyncEither)(ast.decode(a)))), handler => handler(to)));
return (0, _Function.pipe)(getHook(ast), O.match(() => fc => to(fc).filter(a => {
const computed = (0, _ParseResult.eitherOrUndefined)(ast.decode(a));
if (computed) {
return E.isRight(computed);
} else {
return false;
}
}), handler => handler(to)));
}
}
});
};
//# sourceMappingURL=Arbitrary.js.map

@@ -491,3 +491,3 @@ /**

*/
export interface Lazy extends Annotated, HasTransformation {
export interface Lazy extends Annotated {
readonly _tag: "Lazy";

@@ -700,3 +700,2 @@ readonly f: () => AST;

f: () => AST;
hasTransformation: boolean;
} | {

@@ -847,3 +846,2 @@ annotations: {

f: () => AST;
hasTransformation: boolean;
} | {

@@ -850,0 +848,0 @@ annotations: {

@@ -13,3 +13,2 @@ "use strict";

var Order = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/data/typeclass/Order"));
var I = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/internal/common"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

@@ -78,3 +77,3 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

exports.DocumentationAnnotationId = DocumentationAnnotationId;
const hasTransformation = ast => isRefinement(ast) || isTransform(ast) || "hasTransformation" in ast && ast.hasTransformation;
const hasTransformation = ast => isRefinement(ast) || isTransform(ast) || isLazy(ast) || "hasTransformation" in ast && ast.hasTransformation;
/**

@@ -439,4 +438,3 @@ * @since 1.0.0

f,
annotations,
hasTransformation: true
annotations
});

@@ -638,3 +636,3 @@ /**

const getCompiler = match => {
const compile = I.memoize(ast => match[ast._tag](ast, compile));
const compile = ast => match[ast._tag](ast, compile);
return compile;

@@ -650,3 +648,3 @@ };

case "Declaration":
return createDeclaration(ast.typeParameters.map(getTo), ast.type, ast.decode, ast.annotations);
return createDeclaration(ast.typeParameters.map(getTo), getTo(ast.type), ast.decode, ast.annotations);
case "Tuple":

@@ -685,3 +683,3 @@ return createTuple(ast.elements.map(e => ({

case "Declaration":
return createDeclaration(ast.typeParameters.map(getFrom), ast.type, ast.decode, ast.annotations);
return createDeclaration(ast.typeParameters.map(getFrom), getFrom(ast.type), ast.decode, ast.annotations);
case "Tuple":

@@ -691,3 +689,3 @@ return createTuple(ast.elements.map(e => ({

type: getFrom(e.type)
})), O.map(ast.rest, RA.mapNonEmpty(getFrom)), ast.isReadonly, ast.annotations);
})), O.map(ast.rest, RA.mapNonEmpty(getFrom)), ast.isReadonly);
case "TypeLiteral":

@@ -700,7 +698,7 @@ return createTypeLiteral(ast.propertySignatures.map(p => ({

type: getFrom(is.type)
})), ast.annotations);
})));
case "Union":
return createUnion(ast.types.map(getFrom), ast.annotations);
return createUnion(ast.types.map(getFrom));
case "Lazy":
return createLazy(() => getFrom(ast.f()), ast.annotations);
return createLazy(() => getFrom(ast.f()));
case "Refinement":

@@ -726,3 +724,3 @@ case "Transform":

type: reverse(e.type)
})), O.map(ast.rest, RA.mapNonEmpty(reverse)), ast.isReadonly, ast.annotations);
})), O.map(ast.rest, RA.mapNonEmpty(reverse)), ast.isReadonly);
case "TypeLiteral":

@@ -735,9 +733,9 @@ return createTypeLiteral(ast.propertySignatures.map(p => ({

type: reverse(is.type)
})), ast.annotations);
})));
case "Union":
return createUnion(ast.types.map(reverse), ast.annotations);
return createUnion(ast.types.map(reverse));
case "Lazy":
return createLazy(() => reverse(ast.f()), ast.annotations);
return createLazy(() => reverse(ast.f()));
case "Refinement":
return createRefinement(reverse(ast.to), reverse(ast.from), ast.encode, ast.decode, ast.annotations);
return createRefinement(reverse(ast.to), reverse(ast.from), ast.encode, ast.decode);
case "Transform":

@@ -744,0 +742,0 @@ return createTransform(reverse(ast.to), reverse(ast.from), ast.encode, ast.decode);

@@ -6,3 +6,3 @@ "use strict";

});
exports.memoize = exports.makeSchema = exports.getKeysForIndexSignature = exports.PrettyHookId = exports.ArbitraryHookId = void 0;
exports.ownKeys = exports.memoize = exports.makeSchema = exports.getKeysForIndexSignature = exports.PrettyHookId = exports.ArbitraryHookId = void 0;
/**

@@ -47,4 +47,7 @@ * @since 1.0.0

exports.getKeysForIndexSignature = getKeysForIndexSignature;
const ownKeys = o => Object.keys(o).concat(Object.getOwnPropertySymbols(o));
/** @internal */
exports.ownKeys = ownKeys;
const memoize = f => {
const cache = new WeakMap();
const cache = new Map();
return a => {

@@ -51,0 +54,0 @@ if (!cache.has(a)) {

{
"name": "@effect/schema",
"version": "0.3.1",
"version": "0.4.0",
"license": "MIT",

@@ -5,0 +5,0 @@ "repository": {

@@ -60,3 +60,3 @@ /**

*/
export declare const decodeResult: <_, A>(schema: Schema<_, A>) => (i: unknown, options?: ParseOptions | undefined) => ParseResult<A>;
export declare const decodeResult: <I, A>(schema: Schema<I, A>) => (i: I, options?: ParseOptions | undefined) => ParseResult<A>;
/**

@@ -71,3 +71,3 @@ * @category decoding

*/
export declare const decodeEffect: <_, A>(schema: Schema<_, A>) => (i: unknown, options?: ParseOptions | undefined) => Effect.Effect<never, PR.ParseError, A>;
export declare const decodeEffect: <I, A>(schema: Schema<I, A>) => (i: I, options?: ParseOptions | undefined) => Effect.Effect<never, PR.ParseError, A>;
/**

@@ -74,0 +74,0 @@ * @category validation

@@ -28,7 +28,8 @@ "use strict";

const result = parser(input, options);
const resultComputed = PR.eitherOrRunSyncEither(result);
if (E.isLeft(resultComputed)) {
throw new Error((0, _TreeFormatter.formatErrors)(resultComputed.left.errors));
// @ts-expect-error
if (E.isLeft(result)) {
throw new Error((0, _TreeFormatter.formatErrors)(result.left.errors));
}
return resultComputed.right;
// @ts-expect-error
return result.right;
};

@@ -38,15 +39,21 @@ };

const parser = go(ast);
return (input, options) => O.fromEither(PR.eitherOrRunSyncEither(parser(input, options)));
return (input, options) => O.fromEither(parser(input, options));
};
const getEither = ast => {
const parser = go(ast);
return (input, options) => PR.eitherOrRunSyncEither(parser(input, options));
return (input, options) => parser(input, options);
};
const getPromise = ast => {
const parser = go(ast);
return (input, options) => Effect.runPromise(PR.effect(parser(input, options)));
return (input, options) => Effect.runPromise(PR.effect(parser(input, {
...options,
isEffectAllowed: true
})));
};
const getEffect = ast => {
const parser = go(ast);
return (input, options) => PR.effect(parser(input, options));
return (input, options) => PR.effect(parser(input, {
...options,
isEffectAllowed: true
}));
};

@@ -217,6 +224,28 @@ /**

exports.encodeEffect = encodeEffect;
const go = /*#__PURE__*/I.memoize( /*#__PURE__*/(0, _Debug.untracedMethod)(() => ast => {
const go = /*#__PURE__*/(0, _Debug.untracedMethod)(() => (ast, isBoundary = true) => {
if (isBoundary === false && !AST.hasTransformation(ast)) {
return PR.success;
}
switch (ast._tag) {
case "Refinement":
case "Transform":
{
const to = go(ast.to, false);
if (isBoundary) {
const from = go(ast.from);
return (i1, options) => {
const conditional = to === PR.success ? PR.flatMap(from(i1, options), a => ast.decode(a, options)) : PR.flatMap(from(i1, options), a => PR.flatMap(ast.decode(a, options), i2 => to(i2, options)));
const either = PR.eitherOrUndefined(conditional);
return either ? either : options?.isEffectAllowed === true ? conditional : PR.failure(PR.forbidden);
};
} else {
return to === PR.success ? ast.decode : (a, options) => PR.flatMap(ast.decode(a, options), i2 => to(i2, options));
}
}
case "Declaration":
return ast.decode(...ast.typeParameters);
return (i, options) => {
const conditional = ast.decode(...ast.typeParameters)(i, options);
const either = PR.eitherOrUndefined(conditional);
return either ? either : options?.isEffectAllowed === true ? conditional : PR.failure(PR.forbidden);
};
case "Literal":

@@ -256,4 +285,4 @@ return fromRefinement(ast, u => u === ast.literal);

{
const elements = ast.elements.map(e => go(e.type));
const rest = (0, _Function.pipe)(ast.rest, O.map(RA.mapNonEmpty(go)));
const elements = ast.elements.map(e => go(e.type, isBoundary));
const rest = (0, _Function.pipe)(ast.rest, O.map(RA.mapNonEmpty(ast => go(ast))));
let requiredLen = ast.elements.filter(e => !e.isOptional).length;

@@ -475,4 +504,8 @@ if (O.isSome(ast.rest)) {

}
const propertySignaturesTypes = ast.propertySignatures.map(f => go(f.type));
const indexSignatures = ast.indexSignatures.map(is => [go(is.parameter), go(is.type)]);
const propertySignaturesTypes = ast.propertySignatures.map(f => go(f.type, isBoundary));
const indexSignatures = ast.indexSignatures.map(is => [go(is.parameter, isBoundary), go(is.type, isBoundary)]);
const expectedKeys = {};
for (let i = 0; i < propertySignaturesTypes.length; i++) {
expectedKeys[ast.propertySignatures[i].name] = null;
}
return (input, options) => {

@@ -483,3 +516,2 @@ if (!P.isRecord(input)) {

const allErrors = options?.allErrors;
const expectedKeys = {};
const es = [];

@@ -493,3 +525,2 @@ let stepKey = 0;

const name = ps.name;
expectedKeys[name] = null;
if (!Object.prototype.hasOwnProperty.call(input, name)) {

@@ -512,3 +543,3 @@ if (!ps.isOptional) {

if (!isUnexpectedAllowed && indexSignatures.length === 0) {
for (const key of Reflect.ownKeys(input)) {
for (const key of I.ownKeys(input)) {
if (!Object.prototype.hasOwnProperty.call(expectedKeys, key)) {

@@ -603,3 +634,3 @@ const e = PR.key(key, [PR.unexpected(input[key])]);

}
// there's no else here because index signature parameters can't have transformations
// there's no else here because index signature parameters are restricted to primitives
// ---------------------------------------------

@@ -675,8 +706,9 @@ // handle values

const searchTree = _getSearchTree(ast.types);
const ownKeys = Reflect.ownKeys(searchTree.keys);
const ownKeys = I.ownKeys(searchTree.keys);
const len = ownKeys.length;
const map = new Map();
for (let i = 0; i < ast.types.length; i++) {
map.set(ast.types[i], go(ast.types[i]));
map.set(ast.types[i], go(ast.types[i], true)); // <= this must be true
}
return (input, options) => {

@@ -772,15 +804,8 @@ const es = [];

{
const f = () => go(ast.f());
const f = () => go(ast.f(), isBoundary);
const get = I.memoize(f);
return (a, options) => get(f)(a, options);
return (a, options) => get()(a, options);
}
case "Refinement":
case "Transform":
{
const from = go(ast.from);
const to = go(ast.to);
return (i1, options) => PR.flatMap(from(i1, options), a => PR.flatMap(ast.decode(a, options), i2 => to(i2, options)));
}
}
}));
});
const fromRefinement = (ast, refinement) => u => refinement(u) ? PR.success(u) : PR.failure(PR.type(ast, u));

@@ -787,0 +812,0 @@ /** @internal */

@@ -34,3 +34,3 @@ /**

*/
export type ParseErrors = Type | Index | Key | Missing | Unexpected | UnionMember;
export type ParseErrors = Type | Index | Key | Missing | Unexpected | UnionMember | Forbidden;
/**

@@ -53,2 +53,11 @@ * The `Type` variant of the `ParseError` type represents an error that occurs when the `actual` value is not of the expected type.

/**
* The `Forbidden` variant of the `ParseError` type represents an error that occurs when an Effect is encounter but disallowed from execution.
*
* @category model
* @since 1.0.0
*/
export interface Forbidden {
readonly _tag: "Forbidden";
}
/**
* @category constructors

@@ -59,2 +68,7 @@ * @since 1.0.0

/**
* @category constructors
* @since 1.0.0
*/
export declare const forbidden: Forbidden;
/**
* The `Index` decode error indicates that there was an error at a specific index in an array or tuple.

@@ -166,7 +180,2 @@ * The `errors` field contains the decode errors for that index. This error is typically used when decoding an array or tuple

*/
export declare const eitherOrRunSyncEither: <E, A>(self: IO<E, A>) => E.Either<E, A>;
/**
* @category optimisation
* @since 1.0.0
*/
export declare const effect: <E, A>(self: IO<E, A>) => Effect.Effect<never, E, A>;

@@ -173,0 +182,0 @@ /**

@@ -6,3 +6,3 @@ "use strict";

});
exports.unionMember = exports.unexpected = exports.type = exports.success = exports.parseError = exports.missing = exports.map = exports.key = exports.index = exports.flatMap = exports.failures = exports.failure = exports.eitherOrUndefined = exports.eitherOrRunSyncEither = exports.effect = void 0;
exports.unionMember = exports.unexpected = exports.type = exports.success = exports.parseError = exports.missing = exports.map = exports.key = exports.index = exports.forbidden = exports.flatMap = exports.failures = exports.failure = exports.eitherOrUndefined = exports.effect = void 0;
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/data/Either"));

@@ -41,2 +41,10 @@ var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/data/Option"));

exports.type = type;
const forbidden = {
_tag: "Forbidden"
};
/**
* @category constructors
* @since 1.0.0
*/
exports.forbidden = forbidden;
const index = (index, errors) => ({

@@ -107,4 +115,5 @@ _tag: "Index",

const eitherOrUndefined = self => {
if (!Effect.isEffect(self)) {
return self;
const s = self;
if (s["_tag"] === "Left" || s["_tag"] === "Right") {
return s;
}

@@ -117,13 +126,2 @@ };

exports.eitherOrUndefined = eitherOrUndefined;
const eitherOrRunSyncEither = self => {
if (Effect.isEffect(self)) {
return Debug.untraced(() => Effect.runSyncEither(self));
}
return self;
};
/**
* @category optimisation
* @since 1.0.0
*/
exports.eitherOrRunSyncEither = eitherOrRunSyncEither;
const effect = self => {

@@ -140,12 +138,15 @@ if (Effect.isEffect(self)) {

exports.effect = effect;
const flatMap = /*#__PURE__*/Debug.methodWithTrace((trace, restore) => (self, f) => {
if (Effect.isEffect(self)) {
return Effect.flatMap(self, a => effect(restore(f)(a))).traced(trace);
const flatMap = (self, f) => {
const s = self;
if (s["_tag"] === "Left") {
return s;
}
if (E.isRight(self)) {
return restore(f)(self.right);
} else {
return E.left(self.left);
if (s["_tag"] === "Right") {
return f(s.right);
}
});
// @ts-expect-error
return Debug.bodyWithTrace((trace, restore) =>
// @ts-expect-error
Effect.flatMap(self, a => effect(restore(f)(a))).traced(trace));
};
/**

@@ -156,13 +157,14 @@ * @category optimisation

exports.flatMap = flatMap;
const map = /*#__PURE__*/Debug.methodWithTrace((trace, restore) => (self, f) => {
if (Effect.isEffect(self)) {
return Effect.map(self, restore(f)).traced(trace);
const map = (self, f) => {
const s = self;
if (s["_tag"] === "Left") {
return s;
}
if (E.isRight(self)) {
return E.right(restore(f)(self.right));
} else {
return self;
if (s["_tag"] === "Right") {
return E.right(f(s.right));
}
});
// @ts-expect-error
return Debug.bodyWithTrace((trace, restore) => Effect.map(self, restore(f)).traced(trace));
};
exports.map = map;
//# sourceMappingURL=ParseResult.js.map

@@ -105,5 +105,8 @@ "use strict";

const indexSignatureTypes = ast.indexSignatures.map(is => go(is.type));
const expectedKeys = {};
for (let i = 0; i < propertySignaturesTypes.length; i++) {
expectedKeys[ast.propertySignatures[i].name] = null;
}
return input => {
const output = [];
const expectedKeys = {};
// ---------------------------------------------

@@ -119,3 +122,2 @@ // handle property signatures

output.push(`${getPrettyPropertyKey(name)}: ${propertySignaturesTypes[i](input[name])}`);
expectedKeys[name] = null;
}

@@ -150,3 +152,3 @@ // ---------------------------------------------

const get = I.memoize(f);
return a => get(f)(a);
return a => get()(a);
},

@@ -153,0 +155,0 @@ "Refinement": (ast, go) => go(ast.from),

@@ -184,3 +184,2 @@ "use strict";

var _Debug = /*#__PURE__*/require("@effect/io/Debug");
var Effect = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/io/Effect"));
var AST = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/AST"));

@@ -382,3 +381,3 @@ var I = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/internal/common"));

exports.optional = optional;
const struct = fields => make(AST.createTypeLiteral(Reflect.ownKeys(fields).map(key => AST.createPropertySignature(key, fields[key].ast, isOptionalSchema(fields[key]), true)), []));
const struct = fields => make(AST.createTypeLiteral(I.ownKeys(fields).map(key => AST.createPropertySignature(key, fields[key].ast, isOptionalSchema(fields[key]), true)), []));
/**

@@ -575,3 +574,3 @@ * @category combinators

exports.transformEffect = transformEffect;
const transformEither = /*#__PURE__*/(0, _Function.dual)(4, (from, to, decode, encode) => make(AST.createTransform(from.ast, to.ast, (i, o) => Effect.fromEither(decode(i, o)), (i, o) => Effect.fromEither(encode(i, o)))));
const transformEither = /*#__PURE__*/(0, _Function.dual)(4, (from, to, decode, encode) => make(AST.createTransform(from.ast, to.ast, decode, encode)));
/**

@@ -585,3 +584,3 @@ Create a new `Schema` by transforming the input and output of an existing `Schema`

exports.transformEither = transformEither;
const transform = /*#__PURE__*/(0, _Function.dual)(4, (from, to, decode, encode) => transformEffect(from, to, a => PR.success(decode(a)), b => PR.success(encode(b))));
const transform = /*#__PURE__*/(0, _Function.dual)(4, (from, to, decode, encode) => transformEither(from, to, a => E.right(decode(a)), b => E.right(encode(b))));
/**

@@ -909,3 +908,3 @@ * Attaches a property signature with the specified key and value to the schema.

const fromBrand = (constructor, options) => self => {
const decode = (0, _Debug.untracedMethod)(() => a => Effect.fromEither(E.mapLeft(constructor.either(a), brandErrors => PR.parseError([PR.type(ast, a, brandErrors.map(v => v.message).join(", "))]))));
const decode = (0, _Debug.untracedMethod)(() => a => E.mapLeft(constructor.either(a), brandErrors => PR.parseError([PR.type(ast, a, brandErrors.map(v => v.message).join(", "))])));
const ast = AST.createRefinement(self.ast, AST.getTo(self.ast), decode, decode, toAnnotations({

@@ -1380,3 +1379,3 @@ typeId: BrandTypeId,

const propertySignatures = schema.ast.propertySignatures;
const ownKeys = Reflect.ownKeys(fields);
const ownKeys = I.ownKeys(fields);
const from = AST.createTypeLiteral(propertySignatures.concat(ownKeys.map(key => AST.createPropertySignature(key, AST.createUnion([_undefined.ast, _null.ast, fields[key].ast]), true, true))), schema.ast.indexSignatures);

@@ -1383,0 +1382,0 @@ const to = AST.createTypeLiteral(propertySignatures.concat(ownKeys.map(key => AST.createPropertySignature(key, optionFromSelf(fields[key]).ast, true, true))), schema.ast.indexSignatures);

@@ -11,3 +11,3 @@ /**

import * as I from "@effect/schema/internal/common"
import { eitherOrRunSyncEither } from "@effect/schema/ParseResult"
import { eitherOrUndefined } from "@effect/schema/ParseResult"
import type { Schema } from "@effect/schema/Schema"

@@ -63,3 +63,3 @@ import type * as FastCheck from "fast-check"

const go = I.memoize((ast: AST.AST): Arbitrary<any> => {
const go = (ast: AST.AST): Arbitrary<any> => {
switch (ast._tag) {

@@ -195,4 +195,4 @@ case "Declaration":

const f = () => go(ast.f())
const get = I.memoize<typeof f, Arbitrary<any>>(f)
return (fc) => fc.constant(null).chain(() => get(f)(fc))
const get = I.memoize<void, Arbitrary<any>>(f)
return (fc) => fc.constant(null).chain(() => get()(fc))
},

@@ -214,3 +214,12 @@ (handler) => handler()

O.match(
() => (fc) => to(fc).filter((a) => E.isRight(eitherOrRunSyncEither(ast.decode(a)))),
() =>
(fc) =>
to(fc).filter((a) => {
const computed = eitherOrUndefined(ast.decode(a))
if (computed) {
return E.isRight(computed)
} else {
return false
}
}),
(handler) => handler(to)

@@ -221,2 +230,2 @@ )

}
})
}

@@ -13,3 +13,2 @@ /**

import * as Order from "@effect/data/typeclass/Order"
import * as I from "@effect/schema/internal/common"
import type { ParseResult } from "@effect/schema/ParseResult"

@@ -183,3 +182,3 @@

export const hasTransformation = (ast: AST): boolean =>
isRefinement(ast) || isTransform(ast) || (
isRefinement(ast) || isTransform(ast) || isLazy(ast) || (
"hasTransformation" in ast && ast.hasTransformation

@@ -774,3 +773,3 @@ )

*/
export interface Lazy extends Annotated, HasTransformation {
export interface Lazy extends Annotated {
readonly _tag: "Lazy"

@@ -790,4 +789,3 @@ readonly f: () => AST

f,
annotations,
hasTransformation: true
annotations
})

@@ -1055,3 +1053,3 @@

export const getCompiler = <A>(match: Match<A>): Compiler<A> => {
const compile = I.memoize((ast: AST): A => match[ast._tag](ast as any, compile))
const compile = (ast: AST): A => match[ast._tag](ast as any, compile)
return compile

@@ -1069,3 +1067,3 @@ }

ast.typeParameters.map(getTo),
ast.type,
getTo(ast.type),
ast.decode,

@@ -1109,3 +1107,3 @@ ast.annotations

ast.typeParameters.map(getFrom),
ast.type,
getFrom(ast.type),
ast.decode,

@@ -1118,4 +1116,3 @@ ast.annotations

O.map(ast.rest, RA.mapNonEmpty(getFrom)),
ast.isReadonly,
ast.annotations
ast.isReadonly
)

@@ -1125,9 +1122,8 @@ case "TypeLiteral":

ast.propertySignatures.map((p) => ({ ...p, type: getFrom(p.type) })),
ast.indexSignatures.map((is) => ({ ...is, type: getFrom(is.type) })),
ast.annotations
ast.indexSignatures.map((is) => ({ ...is, type: getFrom(is.type) }))
)
case "Union":
return createUnion(ast.types.map(getFrom), ast.annotations)
return createUnion(ast.types.map(getFrom))
case "Lazy":
return createLazy(() => getFrom(ast.f()), ast.annotations)
return createLazy(() => getFrom(ast.f()))
case "Refinement":

@@ -1158,4 +1154,3 @@ case "Transform":

O.map(ast.rest, RA.mapNonEmpty(reverse)),
ast.isReadonly,
ast.annotations
ast.isReadonly
)

@@ -1165,17 +1160,10 @@ case "TypeLiteral":

ast.propertySignatures.map((p) => ({ ...p, type: reverse(p.type) })),
ast.indexSignatures.map((is) => ({ ...is, type: reverse(is.type) })),
ast.annotations
ast.indexSignatures.map((is) => ({ ...is, type: reverse(is.type) }))
)
case "Union":
return createUnion(ast.types.map(reverse), ast.annotations)
return createUnion(ast.types.map(reverse))
case "Lazy":
return createLazy(() => reverse(ast.f()), ast.annotations)
return createLazy(() => reverse(ast.f()))
case "Refinement":
return createRefinement(
reverse(ast.to),
reverse(ast.from),
ast.encode,
ast.decode,
ast.annotations
)
return createRefinement(reverse(ast.to), reverse(ast.from), ast.encode, ast.decode)
case "Transform":

@@ -1182,0 +1170,0 @@ return createTransform(reverse(ast.to), reverse(ast.from), ast.encode, ast.decode)

@@ -48,4 +48,8 @@ /**

/** @internal */
export const memoize = <A extends object, B>(f: (a: A) => B): (a: A) => B => {
const cache = new WeakMap()
export const ownKeys = (o: object): ReadonlyArray<PropertyKey> =>
(Object.keys(o) as ReadonlyArray<PropertyKey>).concat(Object.getOwnPropertySymbols(o))
/** @internal */
export const memoize = <A, B>(f: (a: A) => B): (a: A) => B => {
const cache = new Map()
return (a) => {

@@ -52,0 +56,0 @@ if (!cache.has(a)) {

@@ -26,7 +26,8 @@ /**

const result = parser(input, options)
const resultComputed = PR.eitherOrRunSyncEither(result)
if (E.isLeft(resultComputed)) {
throw new Error(formatErrors(resultComputed.left.errors))
// @ts-expect-error
if (E.isLeft(result)) {
throw new Error(formatErrors(result.left.errors))
}
return resultComputed.right
// @ts-expect-error
return result.right
}

@@ -38,3 +39,3 @@ }

return (input: unknown, options?: ParseOptions) =>
O.fromEither(PR.eitherOrRunSyncEither(parser(input, options)))
O.fromEither<any, any>(parser(input, options) as any)
}

@@ -44,4 +45,3 @@

const parser = go(ast)
return (input: unknown, options?: ParseOptions) =>
PR.eitherOrRunSyncEither(parser(input, options))
return (input: unknown, options?: ParseOptions) => parser(input, options) as any
}

@@ -52,3 +52,3 @@

return (input: unknown, options?: ParseOptions) =>
Effect.runPromise(PR.effect(parser(input, options)))
Effect.runPromise(PR.effect(parser(input, { ...options, isEffectAllowed: true })))
}

@@ -58,3 +58,4 @@

const parser = go(ast)
return (input: unknown, options?: ParseOptions) => PR.effect(parser(input, options))
return (input: unknown, options?: ParseOptions) =>
PR.effect(parser(input, { ...options, isEffectAllowed: true }))
}

@@ -136,5 +137,5 @@

*/
export const decodeResult: <_, A>(
schema: Schema<_, A>
) => (i: unknown, options?: ParseOptions | undefined) => ParseResult<A> = parseResult
export const decodeResult: <I, A>(
schema: Schema<I, A>
) => (i: I, options?: ParseOptions | undefined) => ParseResult<A> = parseResult

@@ -153,5 +154,5 @@ /**

*/
export const decodeEffect: <_, A>(
schema: Schema<_, A>
) => (i: unknown, options?: ParseOptions | undefined) => Effect.Effect<never, PR.ParseError, A> =
export const decodeEffect: <I, A>(
schema: Schema<I, A>
) => (i: I, options?: ParseOptions | undefined) => Effect.Effect<never, PR.ParseError, A> =
parseEffect

@@ -286,11 +287,51 @@

interface ParseEffectOptions extends ParseOptions {
readonly isEffectAllowed?: boolean
}
interface Parser<I, A> {
(i: I, options?: ParseOptions): ParseResult<A>
(i: I, options?: ParseEffectOptions): ParseResult<A>
}
const go = I.memoize(untracedMethod(() =>
(ast: AST.AST): Parser<any, any> => {
const go = untracedMethod(() =>
(ast: AST.AST, isBoundary = true): Parser<any, any> => {
if (isBoundary === false && !AST.hasTransformation(ast)) {
return PR.success
}
switch (ast._tag) {
case "Refinement":
case "Transform": {
const to = go(ast.to, false)
if (isBoundary) {
const from = go(ast.from)
return (i1, options) => {
const conditional = to === PR.success ?
PR.flatMap(from(i1, options), (a) => ast.decode(a, options)) :
PR.flatMap(
from(i1, options),
(a) => PR.flatMap(ast.decode(a, options), (i2) => to(i2, options))
)
const either = PR.eitherOrUndefined(conditional)
return either ?
either :
options?.isEffectAllowed === true ?
conditional :
PR.failure(PR.forbidden)
}
} else {
return to === PR.success ?
ast.decode :
(a, options) => PR.flatMap(ast.decode(a, options), (i2) => to(i2, options))
}
}
case "Declaration":
return ast.decode(...ast.typeParameters)
return (i, options) => {
const conditional = ast.decode(...ast.typeParameters)(i, options)
const either = PR.eitherOrUndefined(conditional)
return either ?
either :
options?.isEffectAllowed === true ?
conditional :
PR.failure(PR.forbidden)
}
case "Literal":

@@ -328,4 +369,4 @@ return fromRefinement(ast, (u): u is typeof ast.literal => u === ast.literal)

case "Tuple": {
const elements = ast.elements.map((e) => go(e.type))
const rest = pipe(ast.rest, O.map(RA.mapNonEmpty(go)))
const elements = ast.elements.map((e) => go(e.type, isBoundary))
const rest = pipe(ast.rest, O.map(RA.mapNonEmpty((ast) => go(ast))))
let requiredLen = ast.elements.filter((e) => !e.isOptional).length

@@ -562,6 +603,10 @@ if (O.isSome(ast.rest)) {

}
const propertySignaturesTypes = ast.propertySignatures.map((f) => go(f.type))
const propertySignaturesTypes = ast.propertySignatures.map((f) => go(f.type, isBoundary))
const indexSignatures = ast.indexSignatures.map((is) =>
[go(is.parameter), go(is.type)] as const
[go(is.parameter, isBoundary), go(is.type, isBoundary)] as const
)
const expectedKeys: any = {}
for (let i = 0; i < propertySignaturesTypes.length; i++) {
expectedKeys[ast.propertySignatures[i].name] = null
}
return (input: unknown, options) => {

@@ -572,3 +617,2 @@ if (!P.isRecord(input)) {

const allErrors = options?.allErrors
const expectedKeys: any = {}
const es: Array<[number, PR.ParseErrors]> = []

@@ -582,3 +626,2 @@ let stepKey = 0

const name = ps.name
expectedKeys[name] = null
if (!Object.prototype.hasOwnProperty.call(input, name)) {

@@ -596,2 +639,3 @@ if (!ps.isOptional) {

}
// ---------------------------------------------

@@ -602,3 +646,3 @@ // handle unexpected keys

if (!isUnexpectedAllowed && indexSignatures.length === 0) {
for (const key of Reflect.ownKeys(input)) {
for (const key of I.ownKeys(input)) {
if (!(Object.prototype.hasOwnProperty.call(expectedKeys, key))) {

@@ -675,2 +719,3 @@ const e = PR.key(key, [PR.unexpected(input[key])])

}
// ---------------------------------------------

@@ -704,3 +749,3 @@ // handle index signatures

}
// there's no else here because index signature parameters can't have transformations
// there's no else here because index signature parameters are restricted to primitives

@@ -783,7 +828,7 @@ // ---------------------------------------------

const searchTree = _getSearchTree(ast.types)
const ownKeys = Reflect.ownKeys(searchTree.keys)
const ownKeys = I.ownKeys(searchTree.keys)
const len = ownKeys.length
const map = new Map<any, Parser<any, any>>()
for (let i = 0; i < ast.types.length; i++) {
map.set(ast.types[i], go(ast.types[i]))
map.set(ast.types[i], go(ast.types[i], true)) // <= this must be true
}

@@ -906,19 +951,9 @@ return (input, options) => {

case "Lazy": {
const f = () => go(ast.f())
const get = I.memoize<typeof f, Parser<any, any>>(f)
return (a, options) => get(f)(a, options)
const f = () => go(ast.f(), isBoundary)
const get = I.memoize<void, Parser<any, any>>(f)
return (a, options) => get()(a, options)
}
case "Refinement":
case "Transform": {
const from = go(ast.from)
const to = go(ast.to)
return (i1, options) =>
PR.flatMap(
from(i1, options),
(a) => PR.flatMap(ast.decode(a, options), (i2) => to(i2, options))
)
}
}
}
))
)

@@ -925,0 +960,0 @@ const fromRefinement = <A>(ast: AST.AST, refinement: (u: unknown) => u is A): Parser<unknown, A> =>

@@ -51,2 +51,3 @@ /**

| UnionMember
| Forbidden

@@ -71,2 +72,12 @@ /**

/**
* The `Forbidden` variant of the `ParseError` type represents an error that occurs when an Effect is encounter but disallowed from execution.
*
* @category model
* @since 1.0.0
*/
export interface Forbidden {
readonly _tag: "Forbidden"
}
/**
* @category constructors

@@ -83,2 +94,10 @@ * @since 1.0.0

/**
* @category constructors
* @since 1.0.0
*/
export const forbidden: Forbidden = {
_tag: "Forbidden"
}
/**
* The `Index` decode error indicates that there was an error at a specific index in an array or tuple.

@@ -225,4 +244,5 @@ * The `errors` field contains the decode errors for that index. This error is typically used when decoding an array or tuple

export const eitherOrUndefined = <E, A>(self: IO<E, A>): E.Either<E, A> | undefined => {
if (!Effect.isEffect(self)) {
return self
const s: any = self
if (s["_tag"] === "Left" || s["_tag"] === "Right") {
return s
}

@@ -235,13 +255,2 @@ }

*/
export const eitherOrRunSyncEither = <E, A>(self: IO<E, A>): E.Either<E, A> => {
if (Effect.isEffect(self)) {
return Debug.untraced(() => Effect.runSyncEither(self))
}
return self
}
/**
* @category optimisation
* @since 1.0.0
*/
export const effect: <E, A>(self: IO<E, A>) => Effect.Effect<never, E, A> = (self) => {

@@ -258,17 +267,19 @@ if (Effect.isEffect(self)) {

*/
export const flatMap = Debug.methodWithTrace((trace, restore) =>
<E, E1, A, B>(
self: IO<E, A>,
f: (self: A) => IO<E1, B>
): IO<E | E1, B> => {
if (Effect.isEffect(self)) {
return Effect.flatMap(self, (a) => effect(restore(f)(a))).traced(trace)
}
if (E.isRight(self)) {
return restore(f)(self.right)
} else {
return E.left(self.left)
}
export const flatMap = <E, E1, A, B>(
self: IO<E, A>,
f: (self: A) => IO<E1, B>
): IO<E | E1, B> => {
const s: any = self
if (s["_tag"] === "Left") {
return s
}
)
if (s["_tag"] === "Right") {
return f(s.right)
}
// @ts-expect-error
return Debug.bodyWithTrace((trace, restore) =>
// @ts-expect-error
Effect.flatMap(self, (a) => effect(restore(f)(a))).traced(trace)
)
}

@@ -279,16 +290,15 @@ /**

*/
export const map = Debug.methodWithTrace((trace, restore) =>
<E, A, B>(
self: IO<E, A>,
f: (self: A) => B
): IO<E, B> => {
if (Effect.isEffect(self)) {
return Effect.map(self, restore(f)).traced(trace)
}
if (E.isRight(self)) {
return E.right(restore(f)(self.right))
} else {
return self
}
export const map = <E, A, B>(
self: IO<E, A>,
f: (self: A) => B
): IO<E, B> => {
const s: any = self
if (s["_tag"] === "Left") {
return s
}
)
if (s["_tag"] === "Right") {
return E.right(f(s.right))
}
// @ts-expect-error
return Debug.bodyWithTrace((trace, restore) => Effect.map(self, restore(f)).traced(trace))
}

@@ -126,5 +126,8 @@ /**

const indexSignatureTypes = ast.indexSignatures.map((is) => go(is.type))
const expectedKeys: any = {}
for (let i = 0; i < propertySignaturesTypes.length; i++) {
expectedKeys[ast.propertySignatures[i].name] = null
}
return (input: { readonly [x: PropertyKey]: unknown }) => {
const output: Array<string> = []
const expectedKeys: any = {}
// ---------------------------------------------

@@ -142,3 +145,2 @@ // handle property signatures

)
expectedKeys[name] = null
}

@@ -173,4 +175,4 @@ // ---------------------------------------------

const f = () => go(ast.f())
const get = I.memoize<typeof f, Pretty<any>>(f)
return (a) => get(f)(a)
const get = I.memoize<void, Pretty<any>>(f)
return (a) => get()(a)
},

@@ -177,0 +179,0 @@ "Refinement": (ast, go) => go(ast.from),

@@ -22,3 +22,2 @@ /**

import { untracedMethod } from "@effect/io/Debug"
import * as Effect from "@effect/io/Effect"
import type { Arbitrary } from "@effect/schema/Arbitrary"

@@ -478,3 +477,3 @@ import type { ParseOptions } from "@effect/schema/AST"

AST.createTypeLiteral(
Reflect.ownKeys(fields).map((key) =>
I.ownKeys(fields).map((key) =>
AST.createPropertySignature(

@@ -826,8 +825,3 @@ key,

encode: (i2: I2, options?: ParseOptions) => E.Either<PR.ParseError, A1>
): Schema<I1, A2> =>
make(
AST.createTransform(from.ast, to.ast, (i, o) =>
Effect.fromEither(decode(i, o)), (i, o) =>
Effect.fromEither(encode(i, o)))
))
): Schema<I1, A2> => make(AST.createTransform(from.ast, to.ast, decode, encode)))

@@ -861,3 +855,3 @@ /**

): Schema<I1, A2> =>
transformEffect(from, to, (a) => PR.success(decode(a)), (b) => PR.success(encode(b)))
transformEither(from, to, (a) => E.right(decode(a)), (b) => E.right(encode(b)))
)

@@ -1278,8 +1272,6 @@

(a: A): ParseResult<C> =>
Effect.fromEither(
E.mapLeft(
constructor.either(a),
(brandErrors) =>
PR.parseError([PR.type(ast, a, brandErrors.map((v) => v.message).join(", "))])
)
E.mapLeft(
constructor.either(a),
(brandErrors) =>
PR.parseError([PR.type(ast, a, brandErrors.map((v) => v.message).join(", "))])
)

@@ -2014,3 +2006,3 @@ )

const propertySignatures = schema.ast.propertySignatures
const ownKeys = Reflect.ownKeys(fields)
const ownKeys = I.ownKeys(fields)
const from = AST.createTypeLiteral(

@@ -2017,0 +2009,0 @@ propertySignatures.concat(

@@ -163,2 +163,4 @@ /**

)
case "Forbidden":
return make("Effect is forbidden from execution")
case "Index": {

@@ -165,0 +167,0 @@ const es = e.errors.map(go)

@@ -119,2 +119,4 @@ "use strict";

return make((0, _Function.pipe)(getMessage(e.expected), O.map(f => f(e.actual)), O.orElse(() => e.message), O.getOrElse(() => `Expected ${formatExpected(e.expected)}, actual ${formatActual(e.actual)}`)));
case "Forbidden":
return make("Effect is forbidden from execution");
case "Index":

@@ -121,0 +123,0 @@ {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc