Socket
Socket
Sign inDemoInstall

@6river/reason-guard

Package Overview
Dependencies
0
Maintainers
5
Versions
52
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.5.4 to 3.6.0

dist/src/ContextError.d.ts

8

dist/src/arrayHasType.js

@@ -6,8 +6,10 @@ "use strict";

const instanceGuards_1 = require("./instanceGuards");
exports.arrayHasType = (itemGuard) => Checker_1.checkerToGuard((input) => {
const ContextError_1 = require("./ContextError");
exports.arrayHasType = (itemGuard) => Checker_1.checkerToGuard((input, context) => {
for (let i = 0; i < input.length; i++) {
const innerErrors = [];
const innerConfs = [];
if (!itemGuard(input[i], innerErrors, innerConfs)) {
throw new Error(`element ${i}: ${innerErrors[0].message}`);
const innerContext = Checker_1.pushContext(i, context);
if (!itemGuard(input[i], innerErrors, innerConfs, innerContext)) {
throw new ContextError_1.CompositeError(innerErrors.map((err) => new ContextError_1.ContextError(`element ${i}: ${err.message}`, err instanceof ContextError_1.ContextError ? err.context : innerContext)));
}

@@ -14,0 +16,0 @@ }

import { NegatableGuard } from './NegatableGuard';
export declare type Checker<FROM> = (input: FROM) => string;
export declare type Checker<FROM> = (input: FROM, context?: PropertyKey[]) => string;
export declare const checkerToGuard: <FROM, TO extends FROM, N extends FROM = FROM>(checker: Checker<FROM>) => NegatableGuard<FROM, TO, N>;
export declare function pushContext<T extends PropertyKey>(p: T, context?: PropertyKey[]): PropertyKey[];
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const NegatableGuard_1 = require("./NegatableGuard");
const ContextError_1 = require("./ContextError");
exports.checkerToGuard = (checker) => NegatableGuard_1.buildNegatable(() => getRawGuard(checker), () => getRawNegation(checker));
function getRawGuard(checker) {
return (input, e = [], c = []) => {
return (input, e = [], c = [], context = []) => {
try {
c.push(checker(input));
c.push(checker(input, context));
return true;
}
catch (err) {
e.push(err);
if (err instanceof ContextError_1.CompositeError) {
e.push(...err.errors);
}
else {
e.push(err);
}
return false;

@@ -35,2 +41,11 @@ }

}
function pushContext(p, context) {
if (context) {
return [...context, p];
}
else {
return [p];
}
}
exports.pushContext = pushContext;
//# sourceMappingURL=Checker.js.map

@@ -8,4 +8,4 @@ "use strict";

function getRawThen(left, right) {
return (input, output, confirmations) => {
return left(input, output, confirmations) && right(input, output, confirmations);
return (input, output, confirmations, context) => {
return left(input, output, confirmations, context) && right(input, output, confirmations, context);
};

@@ -12,0 +12,0 @@ }

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

const primitiveGuards_1 = require("./primitiveGuards");
function checkDefinition(definition, input, output, confirmations) {
function checkDefinition(definition, input, output, confirmations, context) {
let anyPassed = false;

@@ -16,3 +16,3 @@ let anyFailed = false;

if (propertyDefinition) {
if (propertyDefinition(k)(input, output, confirmations)) {
if (propertyDefinition(k)(input, output, confirmations, context)) {
anyPassed = true;

@@ -43,4 +43,4 @@ }

// type PropertyGuardBuilder<FROM, TO extends FROM> = (definition: PropertyGuards<FROM, TO>) => ReasonGuard<FROM, TO>;
exports.objectHasDefinition = ((definition) => (input, output = [], confirmations = []) => checkDefinition(definition, input, output, confirmations));
exports.objectHasDefinition = ((definition) => (input, output = [], confirmations = [], context = []) => checkDefinition(definition, input, output, confirmations, context));
exports.isObjectWithDefinition = (definition) => Combinators_1.thenGuard(primitiveGuards_1.isObject, exports.objectHasDefinition(definition));
//# sourceMappingURL=objectGuards.js.map
import { ReasonGuard } from './ReasonGuard';
import { NegatableGuard } from './NegatableGuard';
export declare type PropertyGuard<DEST_PROP_TYPE> = <T extends string | number | symbol>(p: T) => NegatableGuard<unknown, Record<T, DEST_PROP_TYPE>>;
export declare type StrictOptionalPropertyGuard<DEST_PROP_TYPE> = <T extends string | number | symbol>(p: T) => NegatableGuard<unknown, Partial<Record<T, DEST_PROP_TYPE>>>;
export declare type PropertyKey = string | number | symbol;
export declare type PropertyGuard<DEST_PROP_TYPE> = <T extends PropertyKey>(p: T) => NegatableGuard<unknown, Record<T, DEST_PROP_TYPE>>;
export declare type StrictOptionalPropertyGuard<DEST_PROP_TYPE> = <T extends PropertyKey>(p: T) => NegatableGuard<unknown, Partial<Record<T, DEST_PROP_TYPE>>>;
export declare type OptionalPropertyGuard<DEST_PROP_TYPE> = StrictOptionalPropertyGuard<DEST_PROP_TYPE | undefined>;
export declare type NarrowPropertyGuard<FROM_PROP_TYPE, DEST_PROP_TYPE extends FROM_PROP_TYPE = FROM_PROP_TYPE> = <T extends string | number | symbol>(p: T) => NegatableGuard<Record<T, FROM_PROP_TYPE>, Record<T, DEST_PROP_TYPE>>;
export declare type NarrowPropertyGuard<FROM_PROP_TYPE, DEST_PROP_TYPE extends FROM_PROP_TYPE = FROM_PROP_TYPE> = <T extends PropertyKey>(p: T) => NegatableGuard<Record<T, FROM_PROP_TYPE>, Record<T, DEST_PROP_TYPE>>;
export declare const hasProperty: <T extends string | number | symbol>(p: T) => NegatableGuard<unknown, Record<T, unknown>, Partial<Record<T, never>>>;

@@ -8,0 +9,0 @@ export declare const propertyHasType: <FROMT, T extends string | number | symbol, TOT extends FROMT, TO extends Record<T, TOT>>(itemGuard: ReasonGuard<FROMT, TOT>, p: T) => NegatableGuard<Record<T, FROMT>, Pick<TO, T>, Record<T, FROMT>>;

@@ -7,3 +7,4 @@ "use strict";

const arrayHasType_1 = require("./arrayHasType");
exports.hasProperty = (p) => Checker_1.checkerToGuard((input) => {
const ContextError_1 = require("./ContextError");
exports.hasProperty = (p) => Checker_1.checkerToGuard((input, context) => {
const x = input;

@@ -13,10 +14,11 @@ // if (x[p] === undefined) throw new Error(`property ${p} is undefined`);

if (!(p in x))
throw new Error(`property ${p} is not present`);
throw new ContextError_1.ContextError(`property ${p} is not present`, Checker_1.pushContext(p, context));
return `property ${p} is present`;
});
exports.propertyHasType = (itemGuard, p) => Checker_1.checkerToGuard((input) => {
exports.propertyHasType = (itemGuard, p) => Checker_1.checkerToGuard((input, context) => {
const innerErrors = [];
const innerConfs = [];
if (!itemGuard(input[p], innerErrors, innerConfs)) {
throw new Error(`property ${p}: ${innerErrors[0].message}`);
const innerContext = Checker_1.pushContext(p, context);
if (!itemGuard(input[p], innerErrors, innerConfs, innerContext)) {
throw new ContextError_1.CompositeError(innerErrors.map((err) => new ContextError_1.ContextError(`property ${p}: ${err.message}`, err instanceof ContextError_1.ContextError ? err.context : innerContext)));
}

@@ -23,0 +25,0 @@ return `property ${p}: ${innerConfs[0]}`;

@@ -1,2 +0,2 @@

export declare type ReasonGuard<FROM, TO extends FROM> = (input: FROM, output?: Error[], confirmations?: string[]) => input is TO;
export declare type ReasonGuard<FROM, TO extends FROM> = (input: FROM, output?: Error[], confirmations?: string[], context?: PropertyKey[]) => input is TO;
export declare const cloneGuard: <FROM, TO extends FROM>(g: ReasonGuard<FROM, TO>) => ReasonGuard<FROM, TO>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cloneGuard = (g) => (input, es, cs) => g(input, es, cs);
exports.cloneGuard = (g) => (input, es, cs, context) => g(input, es, cs, context);
//# sourceMappingURL=ReasonGuard.js.map

@@ -55,3 +55,3 @@ {

},
"version": "3.5.4"
"version": "3.6.0"
}
import {ReasonGuard} from './ReasonGuard';
import {checkerToGuard} from './Checker';
import {checkerToGuard, pushContext} from './Checker';
import {thenGuard} from './Combinators';
import {isArray} from './instanceGuards';
import {NegatableGuard} from './NegatableGuard';
import {ContextError, CompositeError} from './ContextError';
export const arrayHasType =
<TO>(itemGuard: ReasonGuard<unknown, TO>) =>
checkerToGuard<unknown[], TO[]>((input: unknown[]) => {
checkerToGuard<unknown[], TO[]>((input: unknown[], context?: PropertyKey[]) => {
for (let i = 0; i < input.length; i++) {
const innerErrors: Error[] = [];
const innerConfs: string[] = [];
if (!itemGuard(input[i], innerErrors, innerConfs)) {
throw new Error(`element ${i}: ${innerErrors[0].message}`);
const innerContext = pushContext(i, context);
if (!itemGuard(input[i], innerErrors, innerConfs, innerContext)) {
throw new CompositeError(innerErrors.map((err) =>
new ContextError(`element ${i}: ${err.message}`,
err instanceof ContextError ? err.context : innerContext))
);
}

@@ -16,0 +21,0 @@ }

import {ReasonGuard} from './ReasonGuard';
import {NegatableGuard, buildNegatable} from './NegatableGuard';
import {CompositeError} from './ContextError';
export type Checker<FROM> = (input: FROM) => string;
export type Checker<FROM> = (input: FROM, context?: PropertyKey[]) => string;

@@ -14,8 +15,12 @@ export const checkerToGuard: <FROM, TO extends FROM, N extends FROM = FROM>(

function getRawGuard<FROM, TO extends FROM>(checker: Checker<FROM>): ReasonGuard<FROM, TO> {
return (input, e = [], c = []): input is TO => {
return (input, e = [], c = [], context = []): input is TO => {
try {
c.push(checker(input));
c.push(checker(input, context));
return true;
} catch (err) {
e.push(err);
if (err instanceof CompositeError) {
e.push(...err.errors);
} else {
e.push(err);
}
return false;

@@ -42,1 +47,9 @@ }

}
export function pushContext<T extends PropertyKey>(p: T, context?: PropertyKey[]): PropertyKey[] {
if (context) {
return [...context, p];
} else {
return [p];
}
}

@@ -18,4 +18,4 @@ import {ReasonGuard} from '../ReasonGuard';

): ReasonGuard<FROM, TO> {
return (input, output, confirmations): input is TO => {
return left(input, output, confirmations) && right(input, output, confirmations);
return (input, output, confirmations, context): input is TO => {
return left(input, output, confirmations, context) && right(input, output, confirmations, context);
};

@@ -22,0 +22,0 @@ }

@@ -41,3 +41,3 @@ import {ReasonGuard} from './ReasonGuard';

function checkDefinition<FROM extends object, TO extends FROM>(
definition: PropertyGuards<FROM, TO>, input: FROM, output: Error[], confirmations: string[],
definition: PropertyGuards<FROM, TO>, input: FROM, output: Error[], confirmations: string[], context?: PropertyKey[]
): input is TO {

@@ -55,3 +55,3 @@ let anyPassed = false;

if (propertyDefinition) {
if (propertyDefinition(k)(input, output, confirmations)) {
if (propertyDefinition(k)(input, output, confirmations, context)) {
anyPassed = true;

@@ -87,3 +87,4 @@ } else {

(definition) =>
(input, output = [], confirmations = []) => checkDefinition(definition, input, output, confirmations)
(input, output = [], confirmations = [], context = []) =>
checkDefinition(definition, input, output, confirmations, context)
);

@@ -90,0 +91,0 @@

import {ReasonGuard} from './ReasonGuard';
import {checkerToGuard} from './Checker';
import {checkerToGuard, pushContext} from './Checker';
import {thenGuard, orGuard, notGuard} from './Combinators';

@@ -7,9 +7,13 @@ import {isUndefined} from './primitiveGuards';

import {NegatableGuard} from './NegatableGuard';
import {ContextError, CompositeError} from './ContextError';
// TODO: we may have this type defined elsewhere already
export type PropertyKey = string|number|symbol;
export type PropertyGuard<DEST_PROP_TYPE> =
<T extends string | number | symbol>(p: T) =>
<T extends PropertyKey>(p: T) =>
NegatableGuard<unknown, Record<T, DEST_PROP_TYPE>>;
export type StrictOptionalPropertyGuard<DEST_PROP_TYPE> =
<T extends string | number | symbol>(p: T) =>
<T extends PropertyKey>(p: T) =>
NegatableGuard<unknown, Partial<Record<T, DEST_PROP_TYPE>>>;

@@ -22,14 +26,17 @@

DEST_PROP_TYPE extends FROM_PROP_TYPE = FROM_PROP_TYPE
> = <T extends string | number | symbol>(p: T) =>
> = <T extends PropertyKey>(p: T) =>
NegatableGuard<Record<T, FROM_PROP_TYPE>, Record<T, DEST_PROP_TYPE>>;
export const hasProperty =
<T extends string | number | symbol>
(p: T) => checkerToGuard<unknown, Record<T, unknown>, Partial<Record<T, never>> >((input: unknown) => {
const x: any = input;
// if (x[p] === undefined) throw new Error(`property ${p} is undefined`);
// if (x[p] === null) throw new Error(`property ${p} is null`); // is this right?
if (!(p in x)) throw new Error(`property ${p} is not present`);
return `property ${p} is present`;
});
<T extends PropertyKey>
(p: T) =>
checkerToGuard<unknown, Record<T, unknown>, Partial<Record<T, never>> >(
(input: unknown, context?: PropertyKey[]) => {
const x: any = input;
// if (x[p] === undefined) throw new Error(`property ${p} is undefined`);
// if (x[p] === null) throw new Error(`property ${p} is null`); // is this right?
if (!(p in x)) throw new ContextError(`property ${p} is not present`, pushContext(p, context));
return `property ${p} is present`;
});

@@ -39,7 +46,10 @@ export const propertyHasType =

(itemGuard: ReasonGuard<FROMT, TOT>, p: T) =>
checkerToGuard<Record<T, FROMT>, Pick<TO, T>>((input) => {
checkerToGuard<Record<T, FROMT>, Pick<TO, T>>((input, context) => {
const innerErrors: Error[] = [];
const innerConfs: string[] = [];
if (!itemGuard(input[p], innerErrors, innerConfs)) {
throw new Error(`property ${p}: ${innerErrors[0].message}`);
const innerContext = pushContext(p, context);
if (!itemGuard(input[p], innerErrors, innerConfs, innerContext)) {
throw new CompositeError(innerErrors.map((err) =>
new ContextError(`property ${p}: ${err.message}`,
err instanceof ContextError ? err.context : innerContext)));
}

@@ -52,3 +62,3 @@ return `property ${p}: ${innerConfs[0]}`;

(g: ReasonGuard<FROM_PROP_TYPE, TO_PROP_TYPE>): NarrowPropertyGuard<FROM_PROP_TYPE, TO_PROP_TYPE> =>
<T extends string | number | symbol>(p: T):
<T extends PropertyKey>(p: T):
NegatableGuard<Record<T, FROM_PROP_TYPE>, Record<T, TO_PROP_TYPE>> =>

@@ -59,3 +69,3 @@ propertyHasType(g, p);

<TO_PROP_TYPE>(g: ReasonGuard<unknown, TO_PROP_TYPE>): PropertyGuard<TO_PROP_TYPE> =>
<T extends string | number | symbol>(p: T):
<T extends PropertyKey>(p: T):
NegatableGuard<unknown, Record<T, TO_PROP_TYPE>> =>

@@ -66,3 +76,3 @@ thenGuard(hasProperty(p), propertyHasType(g, p));

<PTYPE>(g: ReasonGuard<unknown, PTYPE>): OptionalPropertyGuard<PTYPE> =>
<T extends string | number | symbol>(p: T):
<T extends PropertyKey>(p: T):
NegatableGuard<unknown, Partial<Record<T, PTYPE|undefined>>> =>

@@ -79,3 +89,3 @@ orGuard(

<PTYPE>(g: ReasonGuard<unknown, PTYPE>): StrictOptionalPropertyGuard<PTYPE> =>
<T extends string | number | symbol>(p: T):
<T extends PropertyKey>(p: T):
NegatableGuard<unknown, Partial<Record<T, PTYPE>>> =>

@@ -88,5 +98,5 @@ orGuard(

export const hasArrayProperty =
<T extends string | number | symbol, TO>
<T extends PropertyKey, TO>
(itemGuard: ReasonGuard<unknown, TO>) =>
(p: T): NegatableGuard<unknown, Record<T, TO[]>> =>
thenGuard(hasProperty(p), propertyHasType(isArrayOfType(itemGuard), p));

@@ -5,2 +5,3 @@ export type ReasonGuard<FROM, TO extends FROM> = (

confirmations?: string[],
context?: PropertyKey[],
) => input is TO;

@@ -11,3 +12,3 @@

(g: ReasonGuard<FROM, TO>): ReasonGuard<FROM, TO> =>
(input, es, cs): input is TO =>
g(input, es, cs);
(input, es, cs, context): input is TO =>
g(input, es, cs, context);

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc