Comparing version 0.1.9 to 0.1.10
@@ -6,3 +6,2 @@ "use strict"; | ||
const _type_issue_1 = require("./@type-issue"); | ||
const medium_type_1 = require("./medium-type"); | ||
const type_1 = require("./type"); | ||
@@ -68,3 +67,3 @@ const type_partials_1 = require("./type-partials"); | ||
? this.getExactContext(exact, false) | ||
: medium_type_1.DISABLED_EXACT_CONTEXT_RESULT; | ||
: type_1.DISABLED_EXACT_CONTEXT_RESULT; | ||
const unpacked = []; | ||
@@ -71,0 +70,0 @@ const issues = []; |
@@ -5,3 +5,2 @@ "use strict"; | ||
exports.function = exports.fn = exports.FunctionType = void 0; | ||
const errors_1 = require("./errors"); | ||
const type_1 = require("./type"); | ||
@@ -41,3 +40,3 @@ const type_partials_1 = require("./type-partials"); | ||
if (args.length < ArgumentTypeTuple.length) { | ||
throw new errors_1.TypeConstraintError('Failed to call guarded function', [ | ||
throw new type_1.TypeConstraintError('Failed to call guarded function', [ | ||
{ | ||
@@ -51,3 +50,3 @@ path: [], | ||
if (argumentIssues.length > 0) { | ||
throw new errors_1.TypeConstraintError('Failed to call guarded function', argumentIssues); | ||
throw new type_1.TypeConstraintError('Failed to call guarded function', argumentIssues); | ||
} | ||
@@ -57,3 +56,3 @@ const ret = fn(...args.slice(0, ArgumentTypeTuple.length)); | ||
if (returnIssues.length > 0) { | ||
throw new errors_1.TypeConstraintError('Failed to validate guarded function return value', returnIssues); | ||
throw new type_1.TypeConstraintError('Failed to validate guarded function return value', returnIssues); | ||
} | ||
@@ -67,3 +66,3 @@ return ret; | ||
if (args.length < ArgumentTypeTuple.length) { | ||
throw new errors_1.TypeConstraintError('Failed to call guarded function', [ | ||
throw new type_1.TypeConstraintError('Failed to call guarded function', [ | ||
{ | ||
@@ -83,3 +82,3 @@ path: [], | ||
if (argumentIssues.length > 0) { | ||
throw new errors_1.TypeConstraintError('Failed to call guarded function', argumentIssues); | ||
throw new type_1.TypeConstraintError('Failed to call guarded function', argumentIssues); | ||
} | ||
@@ -89,3 +88,3 @@ const ret = fn(...decodedArgs); | ||
if (returnIssues.length > 0) { | ||
throw new errors_1.TypeConstraintError('Failed to validate guarded function return value', returnIssues); | ||
throw new type_1.TypeConstraintError('Failed to validate guarded function return value', returnIssues); | ||
} | ||
@@ -92,0 +91,0 @@ return encodedReturn; |
export * from './medium'; | ||
export * from './type'; | ||
export * from './type-like'; | ||
export * from './medium-type'; | ||
export * from './type'; | ||
export * from './type-partials'; | ||
@@ -18,2 +17,1 @@ export * from './atomic-type'; | ||
export * from './json-schema'; | ||
export * from './errors'; |
@@ -5,5 +5,4 @@ "use strict"; | ||
tslib_1.__exportStar(require("./medium"), exports); | ||
tslib_1.__exportStar(require("./type"), exports); | ||
tslib_1.__exportStar(require("./type-like"), exports); | ||
tslib_1.__exportStar(require("./medium-type"), exports); | ||
tslib_1.__exportStar(require("./type"), exports); | ||
tslib_1.__exportStar(require("./type-partials"), exports); | ||
@@ -22,3 +21,2 @@ tslib_1.__exportStar(require("./atomic-type"), exports); | ||
tslib_1.__exportStar(require("./json-schema"), exports); | ||
tslib_1.__exportStar(require("./errors"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -6,3 +6,2 @@ "use strict"; | ||
const _type_issue_1 = require("./@type-issue"); | ||
const medium_type_1 = require("./medium-type"); | ||
const type_1 = require("./type"); | ||
@@ -54,3 +53,3 @@ const type_partials_1 = require("./type-partials"); | ||
? this.getExactContext(exact, 'managed') | ||
: medium_type_1.DISABLED_EXACT_CONTEXT_RESULT; | ||
: type_1.DISABLED_EXACT_CONTEXT_RESULT; | ||
const partials = []; | ||
@@ -57,0 +56,0 @@ const issues = []; |
@@ -6,3 +6,2 @@ "use strict"; | ||
const _type_issue_1 = require("./@type-issue"); | ||
const medium_type_1 = require("./medium-type"); | ||
const type_1 = require("./type"); | ||
@@ -81,3 +80,3 @@ const type_partials_1 = require("./type-partials"); | ||
? this.getExactContext(exact, false) | ||
: medium_type_1.DISABLED_EXACT_CONTEXT_RESULT; | ||
: type_1.DISABLED_EXACT_CONTEXT_RESULT; | ||
context === null || context === void 0 ? void 0 : context.neutralize(); | ||
@@ -84,0 +83,0 @@ const entries = []; |
import type { JSONSchema } from './json-schema'; | ||
import { Type } from './type'; | ||
import type { TypeInMediumsPartial, TypesInMediums, __type_in_mediums } from './type-partials'; | ||
import type { TypeInMediums, TypeInMediumsPartial, __type_in_mediums } from './type-partials'; | ||
import { __type_kind } from './type-partials'; | ||
export declare class RefinedType<TType extends TypeInMediumsPartial, TNominalKey extends string | symbol, TRefinement> extends Type<RefinedInMediums<TType, TNominalKey, TRefinement>> { | ||
export declare class RefinedType<TType extends TypeInMediumsPartial, TNominalKey extends string | symbol, TRefinement, TGeneral extends boolean = false> extends Type<RefinedInMediums<TType, TNominalKey, TRefinement>, TGeneral> { | ||
private Type; | ||
@@ -14,9 +14,9 @@ private refinements; | ||
declare module './type' { | ||
interface Type<TInMediums> { | ||
refined<TNominalKey extends string | symbol = never, TRefinement = unknown>(refinements: ElementOrArray<Refinement<TInMediums['value']>>, jsonSchema?: JSONSchema): RefinedType<this, TNominalKey, TRefinement>; | ||
nominal<TNominalKey extends string | symbol = never>(jsonSchema?: JSONSchema): RefinedType<this, TNominalKey, unknown>; | ||
interface Type<TInMediums, TGeneral> { | ||
refined<TNominalKey extends string | symbol = never, TRefinement = unknown>(refinements: ElementOrArray<Refinement<TInMediums['value'], TGeneral>>, jsonSchema?: JSONSchema): RefinedType<this, TNominalKey, TRefinement, TGeneral>; | ||
nominal<TNominalKey extends string | symbol = never>(jsonSchema?: JSONSchema): RefinedType<this, TNominalKey, unknown, TGeneral>; | ||
nominalize(value: DenominalizeDeep<TInMediums['value']>): TInMediums['value']; | ||
} | ||
} | ||
export type Refinement<T = unknown> = (value: T) => T; | ||
export type Refinement<T = unknown, TGeneral extends boolean = false> = TGeneral extends true ? (value: unknown) => never : (value: T) => T; | ||
export declare const __nominal: unique symbol; | ||
@@ -38,3 +38,3 @@ export type __nominal = typeof __nominal; | ||
type RefinedInMediums<TType extends TypeInMediumsPartial, TNominalKey extends string | symbol, TRefinement> = __RefinedInMediums<TType[__type_in_mediums], TNominalKey, TRefinement>; | ||
type __RefinedInMediums<TInMediums extends TypesInMediums, TNominalKey extends string | symbol, TRefinement> = { | ||
type __RefinedInMediums<TInMediums extends TypeInMediums, TNominalKey extends string | symbol, TRefinement> = { | ||
[TMediumName in XValue.UsingName]: RefinedMediumType<TInMediums[TMediumName], TNominalKey, TRefinement>; | ||
@@ -41,0 +41,0 @@ }; |
@@ -6,3 +6,2 @@ "use strict"; | ||
const _type_issue_1 = require("./@type-issue"); | ||
const medium_type_1 = require("./medium-type"); | ||
const type_1 = require("./type"); | ||
@@ -90,3 +89,3 @@ const type_partials_1 = require("./type-partials"); | ||
? this.getExactContext(exact, false) | ||
: medium_type_1.DISABLED_EXACT_CONTEXT_RESULT; | ||
: type_1.DISABLED_EXACT_CONTEXT_RESULT; | ||
const unpacked = []; | ||
@@ -93,0 +92,0 @@ const issues = []; |
import type { JSONSchema } from './json-schema'; | ||
import { __type_in_mediums, __type_kind } from './type-partials'; | ||
import type { TypesInMediums } from './type-partials'; | ||
export declare abstract class TypeLike<TInMediums extends TypesInMediums = TypesInMediums> { | ||
import type { TypeInMediums } from './type-partials'; | ||
export declare abstract class TypeLike<TInMediums extends TypeInMediums = TypeInMediums> { | ||
abstract [__type_kind]: string; | ||
@@ -6,0 +6,0 @@ [__type_in_mediums]: TInMediums; |
@@ -8,5 +8,5 @@ export declare const __type_kind: unique symbol; | ||
export type __type_in_mediums = typeof __type_in_mediums; | ||
export type TypesInMediums = Record<XValue.UsingName, unknown>; | ||
export interface TypeInMediumsPartial<TInMediums extends TypesInMediums = TypesInMediums> { | ||
export type TypeInMediums = Record<XValue.UsingName, unknown>; | ||
export interface TypeInMediumsPartial<TInMediums extends TypeInMediums = TypeInMediums> { | ||
[__type_in_mediums]: TInMediums; | ||
} |
@@ -0,6 +1,20 @@ | ||
import type { Exact } from './@exact-context'; | ||
import { ExactContext } from './@exact-context'; | ||
import type { TypeIssue } from './@type-issue'; | ||
import type { JSONSchema } from './json-schema'; | ||
import { MediumType } from './medium-type'; | ||
import type { TypeInMediumsPartial, TypesInMediums, __type_in_mediums } from './type-partials'; | ||
export declare abstract class Type<TInMediums extends TypesInMediums = TypesInMediums> extends MediumType<TInMediums> { | ||
import type { Medium, MediumPackedType } from './medium'; | ||
import { TypeLike } from './type-like'; | ||
import type { TypeInMediums, TypeInMediumsPartial, __type_in_mediums } from './type-partials'; | ||
export declare const DISABLED_EXACT_CONTEXT_RESULT: { | ||
context: undefined; | ||
managedContext: undefined; | ||
wrappedExact: boolean; | ||
nestedExact: boolean; | ||
}; | ||
export declare abstract class Type<TInMediums extends TypeInMediums = TypeInMediums, TGeneral extends boolean = false> extends TypeLike<TInMediums> { | ||
readonly _exact: boolean | undefined; | ||
exact(exact?: boolean): this; | ||
decode<TMedium extends Medium<object>>(medium: TMedium, packed: MediumPackedType<TMedium, TInMediums>): TInMediums['value']; | ||
encode<TMedium extends Medium<object>>(medium: TMedium, value: TInMediums['value']): MediumPackedType<TMedium, TInMediums>; | ||
transform<TFromMedium extends Medium<object>, TToMedium extends Medium<object>>(from: TFromMedium, to: TToMedium, value: MediumPackedType<TFromMedium, TInMediums>): MediumPackedType<TToMedium, TInMediums>; | ||
satisfies(value: unknown): TInMediums['value']; | ||
@@ -11,3 +25,30 @@ asserts(value: unknown): void; | ||
toJSONSchema(): JSONSchema; | ||
protected getExactContext(exact: Exact, wrapper: 'managed'): { | ||
context: ExactContext | undefined; | ||
/** | ||
* Undefined if not exact or inherited (not managed by the current type). | ||
*/ | ||
managedContext: ExactContext | undefined; | ||
wrappedExact: ExactContext | false; | ||
nestedExact: Exact; | ||
}; | ||
protected getExactContext(exact: Exact, wrapper: 'transparent'): { | ||
context: ExactContext | undefined; | ||
managedContext: undefined; | ||
wrappedExact: Exact; | ||
nestedExact: Exact; | ||
}; | ||
protected getExactContext(exact: Exact, wrapper: false): { | ||
context: ExactContext | undefined; | ||
managedContext: undefined; | ||
wrappedExact: false; | ||
nestedExact: Exact; | ||
}; | ||
} | ||
export declare class TypeConstraintError extends TypeError { | ||
private _message; | ||
readonly issues: TypeIssue[]; | ||
constructor(_message: string, issues: TypeIssue[]); | ||
get message(): string; | ||
} | ||
export type TypeOf<TType extends TypeInMediumsPartial> = TType[__type_in_mediums]['value']; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Type = void 0; | ||
const errors_1 = require("./errors"); | ||
const medium_type_1 = require("./medium-type"); | ||
exports.TypeConstraintError = exports.Type = exports.DISABLED_EXACT_CONTEXT_RESULT = void 0; | ||
const _exact_context_1 = require("./@exact-context"); | ||
const type_like_1 = require("./type-like"); | ||
class Type extends medium_type_1.MediumType { | ||
exports.DISABLED_EXACT_CONTEXT_RESULT = { | ||
context: undefined, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: false, | ||
}; | ||
class Type extends type_like_1.TypeLike { | ||
constructor() { | ||
super(...arguments); | ||
Object.defineProperty(this, "_exact", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
} | ||
exact(exact = true) { | ||
return Object.create(this, { | ||
_exact: { value: exact }, | ||
}); | ||
} | ||
decode(medium, packed) { | ||
var _a; | ||
const unpacked = medium.unpack(packed); | ||
const [value, issues] = this._decode(medium, unpacked, [], (_a = this._exact) !== null && _a !== void 0 ? _a : false); | ||
if (issues.length > 0) { | ||
throw new TypeConstraintError('Failed to decode from medium', issues); | ||
} | ||
return value; | ||
} | ||
encode(medium, value) { | ||
var _a; | ||
const [unpacked, issues] = this._encode(medium, value, [], (_a = this._exact) !== null && _a !== void 0 ? _a : false, true); | ||
if (issues.length > 0) { | ||
throw new TypeConstraintError('Failed to encode to medium', issues); | ||
} | ||
return medium.pack(unpacked); | ||
} | ||
transform(from, to, packed) { | ||
var _a; | ||
const unpacked = from.unpack(packed); | ||
const [transformedUnpacked, issues] = this._transform(from, to, unpacked, [], (_a = this._exact) !== null && _a !== void 0 ? _a : false); | ||
if (issues.length > 0) { | ||
throw new TypeConstraintError(`Failed to transform medium`, issues); | ||
} | ||
return to.pack(transformedUnpacked); | ||
} | ||
satisfies(value) { | ||
const issues = this.diagnose(value); | ||
if (issues.length > 0) { | ||
throw new errors_1.TypeConstraintError('Value does not satisfy the type', issues); | ||
throw new TypeConstraintError('Value does not satisfy the type', issues); | ||
} | ||
@@ -33,4 +78,102 @@ return value; | ||
} | ||
getExactContext(exact, wrapper) { | ||
const context = typeof exact === 'boolean' ? undefined : exact; | ||
const selfExact = this._exact; | ||
if (selfExact === false) { | ||
if (context) { | ||
context.neutralize(); | ||
} | ||
return exports.DISABLED_EXACT_CONTEXT_RESULT; | ||
} | ||
if (context) { | ||
if (wrapper) { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: exact, | ||
nestedExact: true, | ||
}; | ||
} | ||
else { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: true, | ||
}; | ||
} | ||
} | ||
else { | ||
if (exact || selfExact) { | ||
if (wrapper === 'managed') { | ||
const context = new _exact_context_1.ExactContext(); | ||
return { | ||
context, | ||
managedContext: context, | ||
wrappedExact: context, | ||
nestedExact: true, | ||
}; | ||
} | ||
else if (wrapper === 'transparent') { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: true, | ||
nestedExact: true, | ||
}; | ||
} | ||
else { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: true, | ||
}; | ||
} | ||
} | ||
else { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: false, | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
exports.Type = Type; | ||
class TypeConstraintError extends TypeError { | ||
constructor(_message, issues) { | ||
super(); | ||
Object.defineProperty(this, "_message", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: _message | ||
}); | ||
Object.defineProperty(this, "issues", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: issues | ||
}); | ||
} | ||
get message() { | ||
return `\ | ||
${this._message}: | ||
${this.issues | ||
.map(({ path, message }) => ` ${path.length > 0 | ||
? `${path | ||
.map(segment => `[${typeof segment === 'object' | ||
? 'key' in segment | ||
? `key:${JSON.stringify(segment.key)}` | ||
: `args[${segment.argument}]` | ||
: JSON.stringify(segment)}]`) | ||
.join('')} ` | ||
: ''}${message}`) | ||
.join('\n')}`; | ||
} | ||
} | ||
exports.TypeConstraintError = TypeConstraintError; | ||
//# sourceMappingURL=type.js.map |
@@ -7,3 +7,2 @@ "use strict"; | ||
const _type_issue_1 = require("./@type-issue"); | ||
const medium_type_1 = require("./medium-type"); | ||
const type_1 = require("./type"); | ||
@@ -64,3 +63,3 @@ const type_partials_1 = require("./type-partials"); | ||
? this.getExactContext(exact, 'transparent') | ||
: medium_type_1.DISABLED_EXACT_CONTEXT_RESULT; | ||
: type_1.DISABLED_EXACT_CONTEXT_RESULT; | ||
let maxIssuePathLength = -1; | ||
@@ -67,0 +66,0 @@ let outputIssues; |
@@ -1,6 +0,6 @@ | ||
import type { MediumType, Type, TypeInMediumsPartial, __type_in_mediums } from './core'; | ||
import type { Type, TypeInMediumsPartial, __type_in_mediums } from './core'; | ||
export type MediumTypeOf<TMediumName extends XValue.UsingName, TType extends TypeInMediumsPartial> = TType[__type_in_mediums][TMediumName]; | ||
export type XTypeOfValue<T> = Type<Record<'value', T> & Record<XValue.UsingName, unknown>>; | ||
export type XTypeOfMediumValue<TMediumName extends XValue.UsingName, T> = MediumType<Record<TMediumName, T> & Record<XValue.UsingName, unknown>>; | ||
export type XTypeOfValue<T> = Type<Record<'value', T> & Record<XValue.UsingName, unknown>, true>; | ||
export type XTypeOfMediumValue<TMediumName extends XValue.UsingName, T> = Type<Record<TMediumName, T> & Record<XValue.UsingName, unknown>, true>; | ||
export declare function constraint(condition: boolean, message?: string | (() => string)): void; | ||
export declare function refinement<T, TRefined extends T>(condition: boolean, refined: T, message?: string | (() => string)): TRefined; | ||
export declare function refinement<T>(condition: boolean, refined: T, message?: string | (() => string)): T; |
import type { RefinedType, TypeOf } from '../core'; | ||
import { number } from '../types'; | ||
export declare const Integer: RefinedType<import("../core").AtomicType<typeof import("../types").numberTypeSymbol>, "integer", unknown>; | ||
export declare const Integer: RefinedType<import("../core").AtomicType<typeof import("../types").numberTypeSymbol>, "integer", unknown, false>; | ||
export type Integer = TypeOf<typeof Integer>; | ||
@@ -5,0 +5,0 @@ export interface IntegerRangeOptions { |
var _a; | ||
import { hasNonDeferrableTypeIssue } from './@type-issue'; | ||
import { DISABLED_EXACT_CONTEXT_RESULT } from './medium-type'; | ||
import { Type } from './type'; | ||
import { DISABLED_EXACT_CONTEXT_RESULT, Type } from './type'; | ||
import { __type_kind } from './type-partials'; | ||
@@ -6,0 +5,0 @@ const toString = Object.prototype.toString; |
var _a; | ||
import { TypeConstraintError } from './errors'; | ||
import { Type } from './type'; | ||
import { Type, TypeConstraintError } from './type'; | ||
import { __type_kind } from './type-partials'; | ||
@@ -5,0 +4,0 @@ const toString = Object.prototype.toString; |
export * from './medium'; | ||
export * from './type'; | ||
export * from './type-like'; | ||
export * from './medium-type'; | ||
export * from './type'; | ||
export * from './type-partials'; | ||
@@ -18,3 +17,2 @@ export * from './atomic-type'; | ||
export * from './json-schema'; | ||
export * from './errors'; | ||
//# sourceMappingURL=index.js.map |
var _a; | ||
import { hasNonDeferrableTypeIssue } from './@type-issue'; | ||
import { DISABLED_EXACT_CONTEXT_RESULT } from './medium-type'; | ||
import { Type } from './type'; | ||
import { DISABLED_EXACT_CONTEXT_RESULT, Type } from './type'; | ||
import { __type_kind } from './type-partials'; | ||
@@ -6,0 +5,0 @@ const hasOwnProperty = Object.prototype.hasOwnProperty; |
var _a; | ||
import { hasNonDeferrableTypeIssue } from './@type-issue'; | ||
import { DISABLED_EXACT_CONTEXT_RESULT } from './medium-type'; | ||
import { Type } from './type'; | ||
import { DISABLED_EXACT_CONTEXT_RESULT, Type } from './type'; | ||
import { __type_kind } from './type-partials'; | ||
@@ -6,0 +5,0 @@ const toString = Object.prototype.toString; |
var _a; | ||
import { hasNonDeferrableTypeIssue } from './@type-issue'; | ||
import { DISABLED_EXACT_CONTEXT_RESULT } from './medium-type'; | ||
import { Type } from './type'; | ||
import { DISABLED_EXACT_CONTEXT_RESULT, Type } from './type'; | ||
import { __type_kind } from './type-partials'; | ||
@@ -6,0 +5,0 @@ const toString = Object.prototype.toString; |
@@ -1,5 +0,50 @@ | ||
import { TypeConstraintError } from './errors'; | ||
import { MediumType } from './medium-type'; | ||
import { JSONSchemaContext } from './type-like'; | ||
export class Type extends MediumType { | ||
import { ExactContext } from './@exact-context'; | ||
import { JSONSchemaContext, TypeLike } from './type-like'; | ||
export const DISABLED_EXACT_CONTEXT_RESULT = { | ||
context: undefined, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: false, | ||
}; | ||
export class Type extends TypeLike { | ||
constructor() { | ||
super(...arguments); | ||
Object.defineProperty(this, "_exact", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
} | ||
exact(exact = true) { | ||
return Object.create(this, { | ||
_exact: { value: exact }, | ||
}); | ||
} | ||
decode(medium, packed) { | ||
var _a; | ||
const unpacked = medium.unpack(packed); | ||
const [value, issues] = this._decode(medium, unpacked, [], (_a = this._exact) !== null && _a !== void 0 ? _a : false); | ||
if (issues.length > 0) { | ||
throw new TypeConstraintError('Failed to decode from medium', issues); | ||
} | ||
return value; | ||
} | ||
encode(medium, value) { | ||
var _a; | ||
const [unpacked, issues] = this._encode(medium, value, [], (_a = this._exact) !== null && _a !== void 0 ? _a : false, true); | ||
if (issues.length > 0) { | ||
throw new TypeConstraintError('Failed to encode to medium', issues); | ||
} | ||
return medium.pack(unpacked); | ||
} | ||
transform(from, to, packed) { | ||
var _a; | ||
const unpacked = from.unpack(packed); | ||
const [transformedUnpacked, issues] = this._transform(from, to, unpacked, [], (_a = this._exact) !== null && _a !== void 0 ? _a : false); | ||
if (issues.length > 0) { | ||
throw new TypeConstraintError(`Failed to transform medium`, issues); | ||
} | ||
return to.pack(transformedUnpacked); | ||
} | ||
satisfies(value) { | ||
@@ -30,3 +75,100 @@ const issues = this.diagnose(value); | ||
} | ||
getExactContext(exact, wrapper) { | ||
const context = typeof exact === 'boolean' ? undefined : exact; | ||
const selfExact = this._exact; | ||
if (selfExact === false) { | ||
if (context) { | ||
context.neutralize(); | ||
} | ||
return DISABLED_EXACT_CONTEXT_RESULT; | ||
} | ||
if (context) { | ||
if (wrapper) { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: exact, | ||
nestedExact: true, | ||
}; | ||
} | ||
else { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: true, | ||
}; | ||
} | ||
} | ||
else { | ||
if (exact || selfExact) { | ||
if (wrapper === 'managed') { | ||
const context = new ExactContext(); | ||
return { | ||
context, | ||
managedContext: context, | ||
wrappedExact: context, | ||
nestedExact: true, | ||
}; | ||
} | ||
else if (wrapper === 'transparent') { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: true, | ||
nestedExact: true, | ||
}; | ||
} | ||
else { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: true, | ||
}; | ||
} | ||
} | ||
else { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: false, | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
export class TypeConstraintError extends TypeError { | ||
constructor(_message, issues) { | ||
super(); | ||
Object.defineProperty(this, "_message", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: _message | ||
}); | ||
Object.defineProperty(this, "issues", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: issues | ||
}); | ||
} | ||
get message() { | ||
return `\ | ||
${this._message}: | ||
${this.issues | ||
.map(({ path, message }) => ` ${path.length > 0 | ||
? `${path | ||
.map(segment => `[${typeof segment === 'object' | ||
? 'key' in segment | ||
? `key:${JSON.stringify(segment.key)}` | ||
: `args[${segment.argument}]` | ||
: JSON.stringify(segment)}]`) | ||
.join('')} ` | ||
: ''}${message}`) | ||
.join('\n')}`; | ||
} | ||
} | ||
//# sourceMappingURL=type.js.map |
var _a; | ||
import { ExactContext } from './@exact-context'; | ||
import { hasNonDeferrableTypeIssue } from './@type-issue'; | ||
import { DISABLED_EXACT_CONTEXT_RESULT } from './medium-type'; | ||
import { Type } from './type'; | ||
import { DISABLED_EXACT_CONTEXT_RESULT, Type } from './type'; | ||
import { __type_kind } from './type-partials'; | ||
@@ -7,0 +6,0 @@ export class UnionType extends Type { |
{ | ||
"name": "x-value", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"repository": "https://github.com/vilic/x-value.git", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -5,4 +5,3 @@ import type {Exact} from './@exact-context'; | ||
import type {Medium} from './medium'; | ||
import {DISABLED_EXACT_CONTEXT_RESULT} from './medium-type'; | ||
import {Type} from './type'; | ||
import {DISABLED_EXACT_CONTEXT_RESULT, Type} from './type'; | ||
import type {JSONSchemaContext, JSONSchemaData} from './type-like'; | ||
@@ -9,0 +8,0 @@ import type {TypeInMediumsPartial, __type_in_mediums} from './type-partials'; |
import type {Exact} from './@exact-context'; | ||
import type {TypeIssue, TypePath} from './@type-issue'; | ||
import {TypeConstraintError} from './errors'; | ||
import type {Medium, UsingMediumName} from './medium'; | ||
import {Type} from './type'; | ||
import {Type, TypeConstraintError} from './type'; | ||
import {__type_kind} from './type-partials'; | ||
@@ -7,0 +6,0 @@ import type {TypeInMediumsPartial, __type_in_mediums} from './type-partials'; |
export * from './medium'; | ||
export * from './type'; | ||
export * from './type-like'; | ||
export * from './medium-type'; | ||
export * from './type'; | ||
export * from './type-partials'; | ||
@@ -18,2 +17,1 @@ export * from './atomic-type'; | ||
export * from './json-schema'; | ||
export * from './errors'; |
@@ -7,4 +7,3 @@ import type {Exact} from './@exact-context'; | ||
import type {Medium} from './medium'; | ||
import {DISABLED_EXACT_CONTEXT_RESULT} from './medium-type'; | ||
import {Type} from './type'; | ||
import {DISABLED_EXACT_CONTEXT_RESULT, Type} from './type'; | ||
import type {JSONSchemaContext, JSONSchemaData} from './type-like'; | ||
@@ -11,0 +10,0 @@ import {__type_kind} from './type-partials'; |
@@ -5,4 +5,3 @@ import type {Exact} from './@exact-context'; | ||
import type {Medium} from './medium'; | ||
import {DISABLED_EXACT_CONTEXT_RESULT} from './medium-type'; | ||
import {Type} from './type'; | ||
import {DISABLED_EXACT_CONTEXT_RESULT, Type} from './type'; | ||
import type {JSONSchemaContext, JSONSchemaData} from './type-like'; | ||
@@ -9,0 +8,0 @@ import type {TypeInMediumsPartial, __type_in_mediums} from './type-partials'; |
@@ -9,4 +9,4 @@ import type {Exact} from './@exact-context'; | ||
import type { | ||
TypeInMediums, | ||
TypeInMediumsPartial, | ||
TypesInMediums, | ||
__type_in_mediums, | ||
@@ -20,3 +20,4 @@ } from './type-partials'; | ||
TRefinement, | ||
> extends Type<RefinedInMediums<TType, TNominalKey, TRefinement>> { | ||
TGeneral extends boolean = false, | ||
> extends Type<RefinedInMediums<TType, TNominalKey, TRefinement>, TGeneral> { | ||
readonly [__type_kind] = 'refined'; | ||
@@ -179,11 +180,11 @@ | ||
declare module './type' { | ||
interface Type<TInMediums> { | ||
interface Type<TInMediums, TGeneral> { | ||
refined<TNominalKey extends string | symbol = never, TRefinement = unknown>( | ||
refinements: ElementOrArray<Refinement<TInMediums['value']>>, | ||
refinements: ElementOrArray<Refinement<TInMediums['value'], TGeneral>>, | ||
jsonSchema?: JSONSchema, | ||
): RefinedType<this, TNominalKey, TRefinement>; | ||
): RefinedType<this, TNominalKey, TRefinement, TGeneral>; | ||
nominal<TNominalKey extends string | symbol = never>( | ||
jsonSchema?: JSONSchema, | ||
): RefinedType<this, TNominalKey, unknown>; | ||
): RefinedType<this, TNominalKey, unknown, TGeneral>; | ||
@@ -212,3 +213,6 @@ nominalize( | ||
export type Refinement<T = unknown> = (value: T) => T; | ||
export type Refinement< | ||
T = unknown, | ||
TGeneral extends boolean = false, | ||
> = TGeneral extends true ? (value: unknown) => never : (value: T) => T; | ||
@@ -242,3 +246,3 @@ export const __nominal = Symbol('nominal'); | ||
type __RefinedInMediums< | ||
TInMediums extends TypesInMediums, | ||
TInMediums extends TypeInMediums, | ||
TNominalKey extends string | symbol, | ||
@@ -245,0 +249,0 @@ TRefinement, |
@@ -6,4 +6,3 @@ import type {Exact} from './@exact-context'; | ||
import type {Medium} from './medium'; | ||
import {DISABLED_EXACT_CONTEXT_RESULT} from './medium-type'; | ||
import {Type} from './type'; | ||
import {DISABLED_EXACT_CONTEXT_RESULT, Type} from './type'; | ||
import type {JSONSchemaContext, JSONSchemaData} from './type-like'; | ||
@@ -10,0 +9,0 @@ import {__type_kind} from './type-partials'; |
@@ -6,6 +6,6 @@ import type {Exact} from './@exact-context'; | ||
import {__type_in_mediums, __type_kind} from './type-partials'; | ||
import type {TypesInMediums} from './type-partials'; | ||
import type {TypeInMediums} from './type-partials'; | ||
export abstract class TypeLike< | ||
TInMediums extends TypesInMediums = TypesInMediums, | ||
TInMediums extends TypeInMediums = TypeInMediums, | ||
> { | ||
@@ -12,0 +12,0 @@ abstract [__type_kind]: string; |
@@ -13,8 +13,8 @@ export const __type_kind = Symbol('type kind'); | ||
export type TypesInMediums = Record<XValue.UsingName, unknown>; | ||
export type TypeInMediums = Record<XValue.UsingName, unknown>; | ||
export interface TypeInMediumsPartial< | ||
TInMediums extends TypesInMediums = TypesInMediums, | ||
TInMediums extends TypeInMediums = TypeInMediums, | ||
> { | ||
[__type_in_mediums]: TInMediums; | ||
} |
@@ -0,15 +1,98 @@ | ||
import type {Exact} from './@exact-context'; | ||
import {ExactContext} from './@exact-context'; | ||
import type {TypeIssue} from './@type-issue'; | ||
import {TypeConstraintError} from './errors'; | ||
import type {JSONSchema} from './json-schema'; | ||
import {MediumType} from './medium-type'; | ||
import {JSONSchemaContext} from './type-like'; | ||
import type {Medium, MediumPackedType} from './medium'; | ||
import {JSONSchemaContext, TypeLike} from './type-like'; | ||
import type { | ||
TypeInMediums, | ||
TypeInMediumsPartial, | ||
TypesInMediums, | ||
__type_in_mediums, | ||
} from './type-partials'; | ||
export const DISABLED_EXACT_CONTEXT_RESULT = { | ||
context: undefined, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: false, | ||
}; | ||
export abstract class Type< | ||
TInMediums extends TypesInMediums = TypesInMediums, | ||
> extends MediumType<TInMediums> { | ||
TInMediums extends TypeInMediums = TypeInMediums, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
TGeneral extends boolean = false, | ||
> extends TypeLike<TInMediums> { | ||
readonly _exact: boolean | undefined; | ||
exact(exact = true): this { | ||
return Object.create(this, { | ||
_exact: {value: exact}, | ||
}); | ||
} | ||
decode<TMedium extends Medium<object>>( | ||
medium: TMedium, | ||
packed: MediumPackedType<TMedium, TInMediums>, | ||
): TInMediums['value']; | ||
decode(medium: Medium, packed: unknown): unknown { | ||
const unpacked = medium.unpack(packed); | ||
const [value, issues] = this._decode( | ||
medium, | ||
unpacked, | ||
[], | ||
this._exact ?? false, | ||
); | ||
if (issues.length > 0) { | ||
throw new TypeConstraintError('Failed to decode from medium', issues); | ||
} | ||
return value; | ||
} | ||
encode<TMedium extends Medium<object>>( | ||
medium: TMedium, | ||
value: TInMediums['value'], | ||
): MediumPackedType<TMedium, TInMediums>; | ||
encode(medium: Medium, value: unknown): unknown { | ||
const [unpacked, issues] = this._encode( | ||
medium, | ||
value, | ||
[], | ||
this._exact ?? false, | ||
true, | ||
); | ||
if (issues.length > 0) { | ||
throw new TypeConstraintError('Failed to encode to medium', issues); | ||
} | ||
return medium.pack(unpacked); | ||
} | ||
transform< | ||
TFromMedium extends Medium<object>, | ||
TToMedium extends Medium<object>, | ||
>( | ||
from: TFromMedium, | ||
to: TToMedium, | ||
value: MediumPackedType<TFromMedium, TInMediums>, | ||
): MediumPackedType<TToMedium, TInMediums>; | ||
transform(from: Medium, to: Medium, packed: unknown): unknown { | ||
const unpacked = from.unpack(packed); | ||
const [transformedUnpacked, issues] = this._transform( | ||
from, | ||
to, | ||
unpacked, | ||
[], | ||
this._exact ?? false, | ||
); | ||
if (issues.length > 0) { | ||
throw new TypeConstraintError(`Failed to transform medium`, issues); | ||
} | ||
return to.pack(transformedUnpacked); | ||
} | ||
satisfies(value: unknown): TInMediums['value'] { | ||
@@ -45,5 +128,141 @@ const issues = this.diagnose(value); | ||
} | ||
protected getExactContext( | ||
exact: Exact, | ||
wrapper: 'managed', | ||
): { | ||
context: ExactContext | undefined; | ||
/** | ||
* Undefined if not exact or inherited (not managed by the current type). | ||
*/ | ||
managedContext: ExactContext | undefined; | ||
wrappedExact: ExactContext | false; | ||
nestedExact: Exact; | ||
}; | ||
protected getExactContext( | ||
exact: Exact, | ||
wrapper: 'transparent', | ||
): { | ||
context: ExactContext | undefined; | ||
managedContext: undefined; | ||
wrappedExact: Exact; | ||
nestedExact: Exact; | ||
}; | ||
protected getExactContext( | ||
exact: Exact, | ||
wrapper: false, | ||
): { | ||
context: ExactContext | undefined; | ||
managedContext: undefined; | ||
wrappedExact: false; | ||
nestedExact: Exact; | ||
}; | ||
protected getExactContext( | ||
exact: Exact, | ||
wrapper: 'managed' | 'transparent' | false, | ||
): { | ||
context: ExactContext | undefined; | ||
managedContext: ExactContext | undefined; | ||
wrappedExact: Exact; | ||
nestedExact: Exact; | ||
} { | ||
const context = typeof exact === 'boolean' ? undefined : exact; | ||
const selfExact = this._exact; | ||
if (selfExact === false) { | ||
if (context) { | ||
context.neutralize(); | ||
} | ||
return DISABLED_EXACT_CONTEXT_RESULT; | ||
} | ||
if (context) { | ||
if (wrapper) { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: exact, | ||
nestedExact: true, | ||
}; | ||
} else { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: true, | ||
}; | ||
} | ||
} else { | ||
if ((exact as boolean) || selfExact) { | ||
if (wrapper === 'managed') { | ||
const context = new ExactContext(); | ||
return { | ||
context, | ||
managedContext: context, | ||
wrappedExact: context, | ||
nestedExact: true, | ||
}; | ||
} else if (wrapper === 'transparent') { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: true, | ||
nestedExact: true, | ||
}; | ||
} else { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: true, | ||
}; | ||
} | ||
} else { | ||
return { | ||
context, | ||
managedContext: undefined, | ||
wrappedExact: false, | ||
nestedExact: false, | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
export class TypeConstraintError extends TypeError { | ||
constructor(private _message: string, readonly issues: TypeIssue[]) { | ||
super(); | ||
} | ||
override get message(): string { | ||
return `\ | ||
${this._message}: | ||
${this.issues | ||
.map( | ||
({path, message}) => | ||
` ${ | ||
path.length > 0 | ||
? `${path | ||
.map( | ||
segment => | ||
`[${ | ||
typeof segment === 'object' | ||
? 'key' in segment | ||
? `key:${JSON.stringify(segment.key)}` | ||
: `args[${segment.argument}]` | ||
: JSON.stringify(segment) | ||
}]`, | ||
) | ||
.join('')} ` | ||
: '' | ||
}${message}`, | ||
) | ||
.join('\n')}`; | ||
} | ||
} | ||
export type TypeOf<TType extends TypeInMediumsPartial> = | ||
TType[__type_in_mediums]['value']; |
@@ -7,4 +7,3 @@ import type {Exact} from './@exact-context'; | ||
import type {Medium} from './medium'; | ||
import {DISABLED_EXACT_CONTEXT_RESULT} from './medium-type'; | ||
import {Type} from './type'; | ||
import {DISABLED_EXACT_CONTEXT_RESULT, Type} from './type'; | ||
import type {JSONSchemaContext, JSONSchemaData} from './type-like'; | ||
@@ -11,0 +10,0 @@ import {__type_kind} from './type-partials'; |
@@ -1,7 +0,2 @@ | ||
import type { | ||
MediumType, | ||
Type, | ||
TypeInMediumsPartial, | ||
__type_in_mediums, | ||
} from './core'; | ||
import type {Type, TypeInMediumsPartial, __type_in_mediums} from './core'; | ||
@@ -14,9 +9,10 @@ export type MediumTypeOf< | ||
export type XTypeOfValue<T> = Type< | ||
Record<'value', T> & Record<XValue.UsingName, unknown> | ||
Record<'value', T> & Record<XValue.UsingName, unknown>, | ||
true | ||
>; | ||
export type XTypeOfMediumValue< | ||
TMediumName extends XValue.UsingName, | ||
T, | ||
> = MediumType<Record<TMediumName, T> & Record<XValue.UsingName, unknown>>; | ||
export type XTypeOfMediumValue<TMediumName extends XValue.UsingName, T> = Type< | ||
Record<TMediumName, T> & Record<XValue.UsingName, unknown>, | ||
true | ||
>; | ||
@@ -36,7 +32,7 @@ export function constraint( | ||
export function refinement<T, TRefined extends T>( | ||
export function refinement<T>( | ||
condition: boolean, | ||
refined: T, | ||
message?: string | (() => string), | ||
): TRefined { | ||
): T { | ||
if (!condition) { | ||
@@ -50,3 +46,3 @@ if (typeof message === 'function') { | ||
return refined as TRefined; | ||
return refined; | ||
} |
@@ -27,3 +27,5 @@ import isEqual from 'lodash.isequal'; | ||
): RefinedType<typeof boolean, never, T>; | ||
export function literal(literal: unknown): TypeInMediumsPartial { | ||
export function literal( | ||
literal: unknown, | ||
): RefinedType<TypeInMediumsPartial, never, unknown, true> { | ||
switch (typeof literal) { | ||
@@ -30,0 +32,0 @@ case 'string': |
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
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
461556
205
8739