@6river/reason-guard
Advanced tools
Comparing version 2.2.0 to 3.0.0
@@ -15,8 +15,15 @@ import { ReasonGuard } from './ReasonGuard'; | ||
*/ | ||
export declare type ChangedFields<FROM, TO extends FROM> = NarrowedFields<FROM, TO> | ExtendedFields<FROM, TO>; | ||
export declare type PropertyGuards<FROM extends Object, TO extends FROM> = { | ||
[P in ChangedFields<FROM, TO>]: P extends keyof FROM ? ReasonGuard<Pick<FROM, P>, Pick<TO, P>> : ReasonGuard<Record<P, unknown>, Pick<TO, P>>; | ||
export declare type ChangedFields<FROM extends object, TO extends FROM> = NarrowedFields<FROM, TO> | ExtendedFields<FROM, TO>; | ||
/** | ||
* A function from property name to guard on that property | ||
*/ | ||
declare type PropertyGuardFactory<FROM extends object, TO extends FROM, P extends ChangedFields<FROM, TO>> = (p: P) => ReasonGuard<Pick<FROM, P & keyof FROM>, Pick<TO, P>>; | ||
/** | ||
* A mapping from property names to factories for guards on those properties | ||
*/ | ||
export declare type PropertyGuards<FROM extends object, TO extends FROM> = { | ||
[P in ChangedFields<FROM, TO>]: PropertyGuardFactory<FROM, TO, P>; | ||
}; | ||
export declare const objectHasDefinition: <FROM extends Object, TO extends FROM>(definition: PropertyGuards<FROM, TO>) => ReasonGuard<FROM, TO>; | ||
export declare const objectHasDefinition: <FROM extends object, TO extends FROM>(definition: PropertyGuards<FROM, TO>) => ReasonGuard<FROM, TO>; | ||
export declare const isObjectWithDefinition: <TO extends object>(definition: PropertyGuards<object, TO>) => import("./NegatableGuard").NegatableGuard<unknown, TO, unknown>; | ||
export {}; |
@@ -5,12 +5,2 @@ "use strict"; | ||
const primitiveGuards_1 = require("./primitiveGuards"); | ||
// this shouldn't be needed, but if the property voodoo goes wrong it might be | ||
// needs to be a factory functionfor the generic parameterization to work? | ||
// export function identityGuard<T>(): ReasonGuard<T, T> { | ||
// return (_input, _output, confirmations): _input is T => { | ||
// confirmations.push('true'); | ||
// return true; | ||
// }; | ||
// } | ||
// don't seem to need this | ||
// type PropertyGuarded<G> = G extends PropertyGuards<infer FROM, infer TO> ? ReasonGuard<FROM, TO> : never; | ||
function checkDefinition(definition, input, output, confirmations) { | ||
@@ -20,3 +10,3 @@ let anyPassed = false; | ||
function checkProperty(k) { | ||
if (definition[k](input, output, confirmations)) { | ||
if (definition[k](k)(input, output, confirmations)) { | ||
anyPassed = true; | ||
@@ -23,0 +13,0 @@ } |
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 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 const hasProperty: <T extends string | number | symbol>(p: T) => NegatableGuard<unknown, Record<T, unknown>, Partial<Record<T, never>>>; | ||
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>>; | ||
export declare const narrowedProperty: <FROM, T extends keyof FROM, TO extends FROM>(p: T, g: ReasonGuard<FROM[T], TO[T]>) => NegatableGuard<Record<T, FROM[T]>, Pick<Record<T, TO[T]>, T>, Record<T, FROM[T]>>; | ||
export declare const requiredProperty: <TO, T extends keyof TO>(p: T, g: ReasonGuard<unknown, TO[T]>) => NegatableGuard<unknown, Pick<TO, T>, unknown>; | ||
export declare type OptionalProps<T> = { | ||
[Key in keyof T]: Pick<T, Key> extends Partial<Pick<T, Key>> ? Partial<Pick<T, Key>> extends Pick<T, Key> ? Key : never : never; | ||
}; | ||
export declare type OptionalKeys<T> = OptionalProps<T>[keyof T] & (string | number | symbol); | ||
export declare const optionalProperty: <TO, T extends OptionalKeys<TO>>(p: T, g: ReasonGuard<unknown, TO[T]>) => NegatableGuard<unknown, Partial<Record<T, never>> | Pick<Record<T, undefined>, T> | Pick<TO, T>, unknown>; | ||
export declare const strictOptionalProperty: <TO, T extends OptionalKeys<TO>>(p: T, g: ReasonGuard<unknown, TO[T]>) => NegatableGuard<unknown, Partial<Record<T, never>> | Pick<TO, T>, unknown>; | ||
export declare const hasNumberProperty: <T extends string | number | symbol>(p: T) => NegatableGuard<unknown, Record<T, number>, unknown>; | ||
export declare const hasStringProperty: <T extends string | number | symbol>(p: T) => NegatableGuard<unknown, Record<T, string>, unknown>; | ||
export declare const hasBooleanProperty: <T extends string | number | symbol>(p: T) => NegatableGuard<unknown, Record<T, boolean>, unknown>; | ||
export declare const hasFunctionProperty: <T extends string | number | symbol>(p: T) => NegatableGuard<unknown, Record<T, Function>, unknown>; | ||
export declare const hasDateProperty: <T extends string | number | symbol>(p: T) => NegatableGuard<unknown, Record<T, Date>, unknown>; | ||
export declare const hasUndefinedProperty: <T extends string | number | symbol>(p: T) => NegatableGuard<unknown, Record<T, undefined>, unknown>; | ||
export declare const hasNullProperty: <T extends string | number | symbol>(p: T) => NegatableGuard<unknown, Record<T, null>, unknown>; | ||
export declare const narrowedProperty: <FROM_PROP_TYPE, TO_PROP_TYPE extends FROM_PROP_TYPE>(g: ReasonGuard<FROM_PROP_TYPE, TO_PROP_TYPE>) => NarrowPropertyGuard<FROM_PROP_TYPE, TO_PROP_TYPE>; | ||
export declare const requiredProperty: <TO_PROP_TYPE>(g: ReasonGuard<unknown, TO_PROP_TYPE>) => PropertyGuard<TO_PROP_TYPE>; | ||
export declare const optionalProperty: <PTYPE>(g: ReasonGuard<unknown, PTYPE>) => StrictOptionalPropertyGuard<PTYPE | undefined>; | ||
export declare const strictOptionalProperty: <PTYPE>(g: ReasonGuard<unknown, PTYPE>) => StrictOptionalPropertyGuard<PTYPE>; | ||
export declare const hasArrayProperty: <T extends string | number | symbol, TO>(itemGuard: ReasonGuard<unknown, TO>) => (p: T) => NegatableGuard<unknown, Record<T, TO[]>, unknown>; |
@@ -6,3 +6,2 @@ "use strict"; | ||
const primitiveGuards_1 = require("./primitiveGuards"); | ||
const instanceGuards_1 = require("./instanceGuards"); | ||
const arrayHasType_1 = require("./arrayHasType"); | ||
@@ -25,26 +24,7 @@ exports.hasProperty = (p) => Checker_1.checkerToGuard((input) => { | ||
}); | ||
const propertyIsUndefined = (p) => Checker_1.checkerToGuard((input) => { | ||
const x = input; | ||
if (x[p] !== undefined) | ||
throw new Error(`property ${p} is not undefined`); | ||
return `property ${p} is undefined`; | ||
}); | ||
const propertyIsNull = (p) => Checker_1.checkerToGuard((input) => { | ||
const x = input; | ||
if (x[p] !== null) | ||
throw new Error(`property ${p} is not null`); | ||
return `property ${p} is null`; | ||
}); | ||
exports.narrowedProperty = (p, g) => exports.propertyHasType(g, p); | ||
exports.requiredProperty = (p, g) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(g, p)); | ||
exports.optionalProperty = (p, g) => Combinators_1.orGuard(Combinators_1.notGuard(exports.hasProperty(p)), Combinators_1.orGuard(exports.requiredProperty(p, primitiveGuards_1.isUndefined), exports.requiredProperty(p, g))); | ||
exports.strictOptionalProperty = (p, g) => Combinators_1.orGuard(Combinators_1.notGuard(exports.hasProperty(p)), exports.requiredProperty(p, g)); | ||
exports.hasNumberProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isNumber, p)); | ||
exports.hasStringProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isString, p)); | ||
exports.hasBooleanProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isBoolean, p)); | ||
exports.hasFunctionProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isFunction, p)); | ||
exports.hasDateProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(instanceGuards_1.isDate, p)); | ||
exports.hasUndefinedProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), propertyIsUndefined(p)); | ||
exports.hasNullProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), propertyIsNull(p)); | ||
exports.narrowedProperty = (g) => (p) => exports.propertyHasType(g, p); | ||
exports.requiredProperty = (g) => (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(g, p)); | ||
exports.optionalProperty = (g) => (p) => Combinators_1.orGuard(Combinators_1.notGuard(exports.hasProperty(p)), Combinators_1.orGuard(exports.requiredProperty(primitiveGuards_1.isUndefined)(p), exports.requiredProperty(g)(p))); | ||
exports.strictOptionalProperty = (g) => (p) => Combinators_1.orGuard(Combinators_1.notGuard(exports.hasProperty(p)), exports.requiredProperty(g)(p)); | ||
exports.hasArrayProperty = (itemGuard) => (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(arrayHasType_1.isArrayOfType(itemGuard), p)); | ||
//# sourceMappingURL=propertyGuards.js.map |
@@ -9,16 +9,16 @@ "use strict"; | ||
const lat = src_1.thenGuard(src_1.isObject, src_1.objectHasDefinition({ | ||
degrees: src_1.requiredProperty('degrees', degLat), | ||
minutes: src_1.requiredProperty('minutes', minSec), | ||
seconds: src_1.requiredProperty('seconds', minSec), | ||
heading: src_1.requiredProperty('heading', src_1.isLiteral(['N', 'S'])), | ||
degrees: src_1.requiredProperty(degLat), | ||
minutes: src_1.requiredProperty(minSec), | ||
seconds: src_1.requiredProperty(minSec), | ||
heading: src_1.requiredProperty(src_1.isLiteral(['N', 'S'])), | ||
})); | ||
const lng = src_1.thenGuard(src_1.isObject, src_1.objectHasDefinition({ | ||
degrees: src_1.requiredProperty('degrees', degLng), | ||
minutes: src_1.requiredProperty('minutes', minSec), | ||
seconds: src_1.requiredProperty('seconds', minSec), | ||
heading: src_1.requiredProperty('heading', src_1.isLiteral(['E', 'W'])), | ||
degrees: src_1.requiredProperty(degLng), | ||
minutes: src_1.requiredProperty(minSec), | ||
seconds: src_1.requiredProperty(minSec), | ||
heading: src_1.requiredProperty(src_1.isLiteral(['E', 'W'])), | ||
})); | ||
const geoCoords = src_1.objectHasDefinition({ | ||
lat: src_1.requiredProperty('lat', lat), | ||
lng: src_1.requiredProperty('lng', lng), | ||
lat: src_1.requiredProperty(lat), | ||
lng: src_1.requiredProperty(lng), | ||
}); | ||
@@ -25,0 +25,0 @@ describe('geographic coordinates', function () { |
@@ -25,9 +25,9 @@ "use strict"; | ||
const guard = src_1.isObjectWithDefinition({ | ||
a: src_1.optionalProperty('a', src_1.isObjectWithDefinition({ | ||
b: src_1.optionalProperty('b', src_1.isString), | ||
a: src_1.optionalProperty(src_1.isObjectWithDefinition({ | ||
b: src_1.optionalProperty(src_1.isString), | ||
})), | ||
}); | ||
const strictGuard = src_1.isObjectWithDefinition({ | ||
a: src_1.strictOptionalProperty('a', src_1.isObjectWithDefinition({ | ||
b: src_1.strictOptionalProperty('b', src_1.isString), | ||
a: src_1.strictOptionalProperty(src_1.isObjectWithDefinition({ | ||
b: src_1.strictOptionalProperty(src_1.isString), | ||
})), | ||
@@ -62,3 +62,3 @@ }); | ||
const guard = src_1.objectHasDefinition({ | ||
b: src_1.requiredProperty('b', src_1.isString), | ||
b: src_1.requiredProperty(src_1.isString), | ||
}); | ||
@@ -73,3 +73,3 @@ it('detects missing extension property', function () { | ||
const guard = src_1.objectHasDefinition({ | ||
a: src_1.requiredProperty('a', src_1.isLiteral(['foo', 'bar'])), | ||
a: src_1.requiredProperty(src_1.isLiteral(['foo', 'bar'])), | ||
}); | ||
@@ -81,3 +81,3 @@ testPropertyGoodValues(guard, { a: 'xyzzy' }, 'a', ['foo', 'bar']); | ||
const guard = src_1.isObjectWithDefinition({ | ||
a: src_1.optionalProperty('a', src_1.orGuard(src_1.isString, src_1.isUndefined)), | ||
a: src_1.optionalProperty(src_1.isString), | ||
}); | ||
@@ -89,3 +89,3 @@ testPropertyGoodValues(guard, {}, 'a', ['foo', undefined]); | ||
const guard = src_1.objectHasDefinition({ | ||
a: src_1.requiredProperty('a', src_1.isString), | ||
a: src_1.requiredProperty(src_1.isString), | ||
}); | ||
@@ -97,6 +97,6 @@ testPropertyGoodValues(guard, {}, 'a', ['foo', 'bar']); | ||
const guard = src_1.objectHasDefinition({ | ||
b: src_1.narrowedProperty('b', src_1.objectHasDefinition({ | ||
d: src_1.requiredProperty('d', src_1.isString), | ||
b: src_1.narrowedProperty(src_1.objectHasDefinition({ | ||
d: src_1.requiredProperty(src_1.isString), | ||
})), | ||
e: src_1.requiredProperty('e', src_1.isString), | ||
e: src_1.requiredProperty(src_1.isString), | ||
}); | ||
@@ -103,0 +103,0 @@ // TODO: we want to assert on _why_ most of these tests pass/fail |
@@ -22,3 +22,3 @@ "use strict"; | ||
it('works normally', function () { | ||
const guard = property.requiredProperty('foo', src_1.isString); | ||
const guard = property.requiredProperty(src_1.isString)('foo'); | ||
assertGuards_1.assertGuards(true)(guard, { foo: 'foo' }); | ||
@@ -29,3 +29,3 @@ assertGuards_1.assertGuards(false)(guard, {}); | ||
it('works negated', function () { | ||
const guard = src_1.notGuard(property.requiredProperty('foo', src_1.isString)); | ||
const guard = src_1.notGuard(property.requiredProperty(src_1.isString)('foo')); | ||
assertGuards_1.assertGuards(!true)(guard, { foo: 'foo' }); | ||
@@ -38,3 +38,3 @@ assertGuards_1.assertGuards(!false)(guard, {}); | ||
it('works normally', function () { | ||
const guard = property.optionalProperty('foo', src_1.isString); | ||
const guard = property.optionalProperty(src_1.isString)('foo'); | ||
assertGuards_1.assertGuards(true)(guard, { foo: 'foo' }); | ||
@@ -45,3 +45,3 @@ assertGuards_1.assertGuards(true)(guard, {}); | ||
it('works negated', function () { | ||
const guard = src_1.notGuard(property.optionalProperty('foo', src_1.isString)); | ||
const guard = src_1.notGuard(property.optionalProperty(src_1.isString)('foo')); | ||
assertGuards_1.assertGuards(!true)(guard, { foo: 'foo' }); | ||
@@ -53,4 +53,4 @@ assertGuards_1.assertGuards(!true)(guard, {}); | ||
const guard = src_1.isObjectWithDefinition({ | ||
foo: property.optionalProperty('foo', src_1.isObjectWithDefinition({ | ||
bar: property.optionalProperty('bar', src_1.isString), | ||
foo: property.optionalProperty(src_1.isObjectWithDefinition({ | ||
bar: property.optionalProperty(src_1.isString), | ||
})), | ||
@@ -73,23 +73,2 @@ }); | ||
}); | ||
context('number property', function () { | ||
testGuardMaker(property.hasNumberProperty, 0); | ||
}); | ||
context('string property', function () { | ||
testGuardMaker(property.hasStringProperty, 1); | ||
}); | ||
context('boolean property', function () { | ||
testGuardMaker(property.hasBooleanProperty, 2); | ||
}); | ||
context('function property', function () { | ||
testGuardMaker(property.hasFunctionProperty, 3); | ||
}); | ||
context('date property', function () { | ||
testGuardMaker(property.hasDateProperty, 4); | ||
}); | ||
context('null property', function () { | ||
testGuardMaker(property.hasNullProperty, 5); | ||
}); | ||
context('undefined property', function () { | ||
testGuardMaker(property.hasUndefinedProperty, 6); | ||
}); | ||
context('array property', function () { | ||
@@ -96,0 +75,0 @@ // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars |
@@ -58,3 +58,3 @@ { | ||
}, | ||
"version": "2.2.0" | ||
"version": "3.0.0" | ||
} |
@@ -17,26 +17,22 @@ import {ReasonGuard} from './ReasonGuard'; | ||
type ExtendedFields<FROM, TO extends FROM> = Exclude<keyof TO, keyof FROM>; | ||
/** | ||
* Fields in `TO` that are different (type or presence) in `FROM` | ||
*/ | ||
export type ChangedFields<FROM, TO extends FROM> = NarrowedFields<FROM, TO> | ExtendedFields<FROM, TO>; | ||
export type ChangedFields<FROM extends object, TO extends FROM> = NarrowedFields<FROM, TO>|ExtendedFields<FROM, TO>; | ||
export type PropertyGuards<FROM extends Object, TO extends FROM> = { | ||
[P in ChangedFields<FROM, TO>]: P extends keyof FROM | ||
? ReasonGuard<Pick<FROM, P>, Pick<TO, P>> | ||
: ReasonGuard<Record<P, unknown>, Pick<TO, P>>; | ||
}; | ||
/** | ||
* A function from property name to guard on that property | ||
*/ | ||
type PropertyGuardFactory<FROM extends object, TO extends FROM, P extends ChangedFields<FROM, TO>> = | ||
(p: P) => ReasonGuard<Pick<FROM, P&keyof FROM>, Pick<TO, P>>; | ||
// this shouldn't be needed, but if the property voodoo goes wrong it might be | ||
// needs to be a factory functionfor the generic parameterization to work? | ||
// export function identityGuard<T>(): ReasonGuard<T, T> { | ||
// return (_input, _output, confirmations): _input is T => { | ||
// confirmations.push('true'); | ||
// return true; | ||
// }; | ||
// } | ||
/** | ||
* A mapping from property names to factories for guards on those properties | ||
*/ | ||
export type PropertyGuards<FROM extends object, TO extends FROM> = { | ||
[P in ChangedFields<FROM, TO>]: PropertyGuardFactory<FROM, TO, P>; | ||
} | ||
// don't seem to need this | ||
// type PropertyGuarded<G> = G extends PropertyGuards<infer FROM, infer TO> ? ReasonGuard<FROM, TO> : never; | ||
function checkDefinition<FROM extends Object, TO extends FROM>( | ||
function checkDefinition<FROM extends object, TO extends FROM>( | ||
definition: PropertyGuards<FROM, TO>, input: FROM, output: Error[], confirmations: string[], | ||
@@ -47,4 +43,4 @@ ): input is TO { | ||
function checkProperty(k: ChangedFields<FROM, TO>) { | ||
if (definition[k](input, output, confirmations)) { | ||
function checkProperty<K extends ChangedFields<FROM, TO>>(k: K) { | ||
if (definition[k](k)(input, output, confirmations)) { | ||
anyPassed = true; | ||
@@ -77,3 +73,3 @@ } else { | ||
export const objectHasDefinition = | ||
<(<FROM extends Object, TO extends FROM>(definition: PropertyGuards<FROM, TO>) => ReasonGuard<FROM, TO>)>( | ||
<(<FROM extends object, TO extends FROM>(definition: PropertyGuards<FROM, TO>) => ReasonGuard<FROM, TO>)>( | ||
(definition) => | ||
@@ -80,0 +76,0 @@ (input, output = [], confirmations = []) => checkDefinition(definition, input, output, confirmations) |
import {ReasonGuard} from './ReasonGuard'; | ||
import {checkerToGuard} from './Checker'; | ||
import {thenGuard, orGuard, notGuard} from './Combinators'; | ||
import {isNumber, isString, isBoolean, isFunction, isUndefined} from './primitiveGuards'; | ||
import {isDate} from './instanceGuards'; | ||
import {isUndefined} from './primitiveGuards'; | ||
import {isArrayOfType} from './arrayHasType'; | ||
import {NegatableGuard} from './NegatableGuard'; | ||
export type PropertyGuard<DEST_PROP_TYPE> = | ||
<T extends string | number | symbol>(p: T) => | ||
NegatableGuard<unknown, Record<T, DEST_PROP_TYPE>>; | ||
export type StrictOptionalPropertyGuard<DEST_PROP_TYPE> = | ||
<T extends string | number | symbol>(p: T) => | ||
NegatableGuard<unknown, Partial<Record<T, DEST_PROP_TYPE>>>; | ||
export type OptionalPropertyGuard<DEST_PROP_TYPE> = StrictOptionalPropertyGuard<DEST_PROP_TYPE|undefined>; | ||
export 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 const hasProperty = | ||
@@ -31,84 +46,40 @@ <T extends string | number | symbol> | ||
const propertyIsUndefined = | ||
<T extends string | number | symbol>(p: T) => | ||
checkerToGuard<Record<T, unknown>, Record<T, undefined>>((input: unknown) => { | ||
const x: any = input; | ||
if (x[p] !== undefined) throw new Error(`property ${p} is not undefined`); | ||
return `property ${p} is undefined`; | ||
}); | ||
const propertyIsNull = | ||
<T extends string | number | symbol>(p: T) => | ||
checkerToGuard<Record<T, unknown>, Record<T, null>>((input: unknown) => { | ||
const x: any = input; | ||
if (x[p] !== null) throw new Error(`property ${p} is not null`); | ||
return `property ${p} is null`; | ||
}); | ||
export const narrowedProperty = | ||
<FROM, T extends keyof FROM, TO extends FROM> | ||
(p: T, g: ReasonGuard<FROM[T], TO[T]>) => | ||
propertyHasType(g, p); | ||
<FROM_PROP_TYPE, TO_PROP_TYPE extends FROM_PROP_TYPE> | ||
(g: ReasonGuard<FROM_PROP_TYPE, TO_PROP_TYPE>): NarrowPropertyGuard<FROM_PROP_TYPE, TO_PROP_TYPE> => | ||
<T extends string | number | symbol>(p: T): | ||
NegatableGuard<Record<T, FROM_PROP_TYPE>, Record<T, TO_PROP_TYPE>> => | ||
propertyHasType(g, p); | ||
export const requiredProperty = | ||
<TO, T extends keyof TO> | ||
(p: T, g: ReasonGuard<unknown, TO[T]>): NegatableGuard<unknown, Pick<TO, T>, unknown> => | ||
thenGuard(hasProperty(p), propertyHasType<unknown, T, TO[T], TO>(g, p)); | ||
<TO_PROP_TYPE>(g: ReasonGuard<unknown, TO_PROP_TYPE>): PropertyGuard<TO_PROP_TYPE> => | ||
<T extends string | number | symbol>(p: T): | ||
NegatableGuard<unknown, Record<T, TO_PROP_TYPE>> => | ||
thenGuard(hasProperty(p), propertyHasType(g, p)); | ||
export type OptionalProps<T> = { | ||
[Key in keyof T]: | ||
Pick<T, Key> extends Partial<Pick<T, Key>> | ||
? Partial<Pick<T, Key>> extends Pick<T, Key> | ||
? Key | ||
: never | ||
: never | ||
}; | ||
export type OptionalKeys<T> = | ||
OptionalProps<T>[keyof T] & (string|number|symbol); | ||
export const optionalProperty = | ||
<TO, T extends OptionalKeys<TO>> | ||
(p: T, g: ReasonGuard<unknown, TO[T]>) => | ||
orGuard( | ||
notGuard(hasProperty(p)), | ||
<PTYPE>(g: ReasonGuard<unknown, PTYPE>): OptionalPropertyGuard<PTYPE> => | ||
<T extends string | number | symbol>(p: T): | ||
NegatableGuard<unknown, Partial<Record<T, PTYPE|undefined>>> => | ||
orGuard( | ||
requiredProperty<Record<T, undefined>, T>(p, isUndefined), | ||
requiredProperty(p, g) | ||
) | ||
); | ||
notGuard(hasProperty(p)), | ||
orGuard( | ||
requiredProperty(isUndefined)(p), | ||
requiredProperty(g)(p) | ||
) | ||
); | ||
export const strictOptionalProperty = | ||
<TO, T extends OptionalKeys<TO>> | ||
(p: T, g: ReasonGuard<unknown, TO[T]>) => | ||
orGuard( | ||
notGuard(hasProperty(p)), | ||
requiredProperty(p, g) | ||
); | ||
<PTYPE>(g: ReasonGuard<unknown, PTYPE>): StrictOptionalPropertyGuard<PTYPE> => | ||
<T extends string | number | symbol>(p: T): | ||
NegatableGuard<unknown, Partial<Record<T, PTYPE>>> => | ||
orGuard( | ||
notGuard(hasProperty(p)), | ||
requiredProperty(g)(p) | ||
); | ||
export const hasNumberProperty = | ||
<T extends string | number | symbol>(p: T): NegatableGuard<unknown, Record<T, number>, unknown> => | ||
thenGuard(hasProperty(p), propertyHasType(isNumber, p)); | ||
export const hasStringProperty = | ||
<T extends string | number | symbol>(p: T): NegatableGuard<unknown, Record<T, string>, unknown> => | ||
thenGuard(hasProperty(p), propertyHasType(isString, p)); | ||
export const hasBooleanProperty = | ||
<T extends string | number | symbol>(p: T): NegatableGuard<unknown, Record<T, boolean>, unknown> => | ||
thenGuard(hasProperty(p), propertyHasType(isBoolean, p)); | ||
export const hasFunctionProperty = | ||
<T extends string | number | symbol>(p: T): NegatableGuard<unknown, Record<T, Function>, unknown> => | ||
thenGuard(hasProperty(p), propertyHasType(isFunction, p)); | ||
export const hasDateProperty = | ||
<T extends string | number | symbol>(p: T): NegatableGuard<unknown, Record<T, Date>, unknown> => | ||
thenGuard(hasProperty(p), propertyHasType(isDate, p)); | ||
export const hasUndefinedProperty = | ||
<T extends string | number | symbol>(p: T): NegatableGuard<unknown, Record<T, undefined>, unknown> => | ||
thenGuard(hasProperty(p), propertyIsUndefined(p)); | ||
export const hasNullProperty = | ||
<T extends string | number | symbol>(p: T): NegatableGuard<unknown, Record<T, null>, unknown> => | ||
thenGuard(hasProperty(p), propertyIsNull(p)); | ||
export const hasArrayProperty = | ||
<T extends string | number | symbol, TO> | ||
(itemGuard: ReasonGuard<unknown, TO>) => | ||
(p: T): NegatableGuard<unknown, Record<T, TO[]>, unknown> => | ||
(p: T): NegatableGuard<unknown, Record<T, TO[]>> => | ||
thenGuard(hasProperty(p), propertyHasType(isArrayOfType(itemGuard), p)); |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
231827
1852