@app-config/extension-utils
Advanced tools
Comparing version 3.0.0-alpha.3 to 3.0.0-alpha.4
@@ -1,2 +0,2 @@ | ||
import type { ParsingExtension, ParsingExtensionKey } from '@app-config/core'; | ||
import type { ParsingContext, ParsingExtension, ParsingExtensionKey, ParsingExtensionTransform } from '@app-config/core'; | ||
import { AppConfigError } from '@app-config/core'; | ||
@@ -6,9 +6,11 @@ import { Json } from '@app-config/utils'; | ||
export declare function composeExtensions(extensions: ParsingExtension[]): ParsingExtension; | ||
export declare function named(name: string, parsingExtension: ParsingExtension): ParsingExtension; | ||
export declare function forKey(key: string | string[], parsingExtension: ParsingExtension): ParsingExtension; | ||
export declare function keysToPath(keys: ParsingExtensionKey[]): string; | ||
export declare class ParsingExtensionInvalidOptions extends AppConfigError { | ||
} | ||
export declare function validateOptions<T extends Json>(builder: (builder: typeof SchemaBuilder) => SchemaBuilder<T>, extension: ParsingExtension<T>, { lazy }?: { | ||
export declare function validateOptions<T extends Json>(builder: (builder: typeof SchemaBuilder) => SchemaBuilder<T>, extension: (value: T, key: ParsingExtensionKey, parentKeys: ParsingExtensionKey[], context: ParsingContext) => ParsingExtensionTransform | false, { lazy }?: { | ||
lazy?: boolean; | ||
}): ParsingExtension; | ||
export declare type ValidationFunction<T> = (value: any, ctx: ParsingExtensionKey[]) => asserts value is T; | ||
export declare type ValidationFunction<T> = (value: any, parentKeys: ParsingExtensionKey[]) => asserts value is T; | ||
export declare function validationFunction<T>(builder: (builder: typeof SchemaBuilder) => SchemaBuilder<T>): ValidationFunction<T>; |
import { parseValue, Root, AppConfigError } from '@app-config/core'; | ||
import { SchemaBuilder } from '@serafin/schema-builder'; | ||
export function composeExtensions(extensions) { | ||
return (value, [[k]]) => { | ||
const composed = (value, [k], _, context) => { | ||
// only applies to the root - override the parsing extensions | ||
if (k !== Root) | ||
return false; | ||
return (_, __, source) => parseValue(value, source, extensions, { shouldFlatten: true }); | ||
return (_, __, source, baseExtensions) => | ||
// restart the parse tree, but with additional extensions included | ||
parseValue(value, source, | ||
// ensures that a recursion doesn't happen | ||
baseExtensions.concat(extensions).filter((v) => v !== composed), { shouldFlatten: true }, context); | ||
}; | ||
return composed; | ||
} | ||
export function named(name, parsingExtension) { | ||
Object.defineProperty(parsingExtension, 'extensionName', { value: name }); | ||
return parsingExtension; | ||
} | ||
export function forKey(key, parsingExtension) { | ||
@@ -19,5 +29,5 @@ const shouldApply = ([_, k]) => { | ||
}; | ||
return (value, parentKeys, context) => { | ||
if (shouldApply(parentKeys[0])) { | ||
return parsingExtension(value, parentKeys, context); | ||
return (value, currentKey, parentKeys, context) => { | ||
if (shouldApply(currentKey)) { | ||
return parsingExtension(value, currentKey, parentKeys, context); | ||
} | ||
@@ -27,2 +37,10 @@ return false; | ||
} | ||
export function keysToPath(keys) { | ||
if (keys.length === 0) | ||
return 'root'; | ||
return (keys | ||
.map(([, k]) => k) | ||
.filter((v) => v) | ||
.join('.') || 'root'); | ||
} | ||
export class ParsingExtensionInvalidOptions extends AppConfigError { | ||
@@ -32,3 +50,3 @@ } | ||
const validate = validationFunction(builder); | ||
return (value, parentKeys, context) => { | ||
return (value, key, parentKeys, context) => { | ||
return async (parse, ...args) => { | ||
@@ -42,4 +60,4 @@ let valid; | ||
} | ||
validate(valid, parentKeys); | ||
const call = extension(valid, parentKeys, context); | ||
validate(valid, [...parentKeys, key]); | ||
const call = extension(valid, key, parentKeys, context); | ||
if (call) { | ||
@@ -55,3 +73,3 @@ return call(parse, ...args); | ||
schema.cacheValidationFunction(); | ||
return (value, ctx) => { | ||
return (value, parentKeys) => { | ||
try { | ||
@@ -62,8 +80,3 @@ schema.validate(value); | ||
const message = error instanceof Error ? error.message : 'unknown'; | ||
const parents = [...ctx] | ||
.reverse() | ||
.map(([, k]) => k) | ||
.filter((v) => !!v) | ||
.join('.') || 'root'; | ||
throw new ParsingExtensionInvalidOptions(`Validation failed in "${parents}": ${message}`); | ||
throw new ParsingExtensionInvalidOptions(`Validation failed in "${keysToPath(parentKeys)}": ${message}`); | ||
} | ||
@@ -70,0 +83,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import type { ParsingExtension, ParsingExtensionKey } from '@app-config/core'; | ||
import type { ParsingContext, ParsingExtension, ParsingExtensionKey, ParsingExtensionTransform } from '@app-config/core'; | ||
import { AppConfigError } from '@app-config/core'; | ||
@@ -6,9 +6,11 @@ import { Json } from '@app-config/utils'; | ||
export declare function composeExtensions(extensions: ParsingExtension[]): ParsingExtension; | ||
export declare function named(name: string, parsingExtension: ParsingExtension): ParsingExtension; | ||
export declare function forKey(key: string | string[], parsingExtension: ParsingExtension): ParsingExtension; | ||
export declare function keysToPath(keys: ParsingExtensionKey[]): string; | ||
export declare class ParsingExtensionInvalidOptions extends AppConfigError { | ||
} | ||
export declare function validateOptions<T extends Json>(builder: (builder: typeof SchemaBuilder) => SchemaBuilder<T>, extension: ParsingExtension<T>, { lazy }?: { | ||
export declare function validateOptions<T extends Json>(builder: (builder: typeof SchemaBuilder) => SchemaBuilder<T>, extension: (value: T, key: ParsingExtensionKey, parentKeys: ParsingExtensionKey[], context: ParsingContext) => ParsingExtensionTransform | false, { lazy }?: { | ||
lazy?: boolean; | ||
}): ParsingExtension; | ||
export declare type ValidationFunction<T> = (value: any, ctx: ParsingExtensionKey[]) => asserts value is T; | ||
export declare type ValidationFunction<T> = (value: any, parentKeys: ParsingExtensionKey[]) => asserts value is T; | ||
export declare function validationFunction<T>(builder: (builder: typeof SchemaBuilder) => SchemaBuilder<T>): ValidationFunction<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validationFunction = exports.validateOptions = exports.ParsingExtensionInvalidOptions = exports.forKey = exports.composeExtensions = void 0; | ||
exports.validationFunction = exports.validateOptions = exports.ParsingExtensionInvalidOptions = exports.keysToPath = exports.forKey = exports.named = exports.composeExtensions = void 0; | ||
const core_1 = require("@app-config/core"); | ||
const schema_builder_1 = require("@serafin/schema-builder"); | ||
function composeExtensions(extensions) { | ||
return (value, [[k]]) => { | ||
const composed = (value, [k], _, context) => { | ||
// only applies to the root - override the parsing extensions | ||
if (k !== core_1.Root) | ||
return false; | ||
return (_, __, source) => core_1.parseValue(value, source, extensions, { shouldFlatten: true }); | ||
return (_, __, source, baseExtensions) => | ||
// restart the parse tree, but with additional extensions included | ||
core_1.parseValue(value, source, | ||
// ensures that a recursion doesn't happen | ||
baseExtensions.concat(extensions).filter((v) => v !== composed), { shouldFlatten: true }, context); | ||
}; | ||
return composed; | ||
} | ||
exports.composeExtensions = composeExtensions; | ||
function named(name, parsingExtension) { | ||
Object.defineProperty(parsingExtension, 'extensionName', { value: name }); | ||
return parsingExtension; | ||
} | ||
exports.named = named; | ||
function forKey(key, parsingExtension) { | ||
@@ -23,5 +34,5 @@ const shouldApply = ([_, k]) => { | ||
}; | ||
return (value, parentKeys, context) => { | ||
if (shouldApply(parentKeys[0])) { | ||
return parsingExtension(value, parentKeys, context); | ||
return (value, currentKey, parentKeys, context) => { | ||
if (shouldApply(currentKey)) { | ||
return parsingExtension(value, currentKey, parentKeys, context); | ||
} | ||
@@ -32,2 +43,11 @@ return false; | ||
exports.forKey = forKey; | ||
function keysToPath(keys) { | ||
if (keys.length === 0) | ||
return 'root'; | ||
return (keys | ||
.map(([, k]) => k) | ||
.filter((v) => v) | ||
.join('.') || 'root'); | ||
} | ||
exports.keysToPath = keysToPath; | ||
class ParsingExtensionInvalidOptions extends core_1.AppConfigError { | ||
@@ -38,3 +58,3 @@ } | ||
const validate = validationFunction(builder); | ||
return (value, parentKeys, context) => { | ||
return (value, key, parentKeys, context) => { | ||
return async (parse, ...args) => { | ||
@@ -48,4 +68,4 @@ let valid; | ||
} | ||
validate(valid, parentKeys); | ||
const call = extension(valid, parentKeys, context); | ||
validate(valid, [...parentKeys, key]); | ||
const call = extension(valid, key, parentKeys, context); | ||
if (call) { | ||
@@ -62,3 +82,3 @@ return call(parse, ...args); | ||
schema.cacheValidationFunction(); | ||
return (value, ctx) => { | ||
return (value, parentKeys) => { | ||
try { | ||
@@ -69,8 +89,3 @@ schema.validate(value); | ||
const message = error instanceof Error ? error.message : 'unknown'; | ||
const parents = [...ctx] | ||
.reverse() | ||
.map(([, k]) => k) | ||
.filter((v) => !!v) | ||
.join('.') || 'root'; | ||
throw new ParsingExtensionInvalidOptions(`Validation failed in "${parents}": ${message}`); | ||
throw new ParsingExtensionInvalidOptions(`Validation failed in "${keysToPath(parentKeys)}": ${message}`); | ||
} | ||
@@ -77,0 +92,0 @@ }; |
{ | ||
"name": "@app-config/extension-utils", | ||
"description": "Utilities for writing @app-config parsing extensions", | ||
"version": "3.0.0-alpha.3", | ||
"version": "3.0.0-alpha.4", | ||
"license": "MPL-2.0", | ||
@@ -33,3 +33,3 @@ "author": { | ||
"dependencies": { | ||
"@app-config/core": "^3.0.0-alpha.3", | ||
"@app-config/core": "^3.0.0-alpha.4", | ||
"@serafin/schema-builder": "0.14" | ||
@@ -36,0 +36,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
15456
196