zerde 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +74 -36
- package/dist/index.js +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,28 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ParseOptions as ParseOptions$1 } from "jsonc-parser";
|
|
2
|
+
import { CreateNodeOptions, DocumentOptions, ParseOptions as ParseOptions$2, SchemaOptions, ToJSOptions, ToStringOptions } from "yaml";
|
|
3
|
+
import * as neverthrow4 from "neverthrow";
|
|
4
|
+
import * as neverthrow3 from "neverthrow";
|
|
2
5
|
import * as neverthrow0 from "neverthrow";
|
|
3
|
-
import * as neverthrow1 from "neverthrow";
|
|
4
6
|
import { ResultAsync } from "neverthrow";
|
|
5
|
-
import {
|
|
6
|
-
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
7
|
+
import { StandardSchemaV1, StandardSchemaV1 as StandardSchemaV1$1 } from "@standard-schema/spec";
|
|
7
8
|
|
|
8
9
|
//#region src/formats/json.d.ts
|
|
9
10
|
type ParseJSONOptions = Partial<ParseOptions$1>;
|
|
11
|
+
declare const defaultParseJSONOptions: {
|
|
12
|
+
allowEmptyContent: false;
|
|
13
|
+
allowTrailingComma: false;
|
|
14
|
+
disallowComments: true;
|
|
15
|
+
};
|
|
16
|
+
declare function parseJSON(stringifiedContent: string, parseOptions?: ParseJSONOptions): unknown;
|
|
10
17
|
type StringifyJSONOptions = string | number;
|
|
18
|
+
declare const defaultStringifyJSONOptions: 0;
|
|
19
|
+
declare function stringifyJSON(unknownContent: unknown, stringifyOptions?: StringifyJSONOptions): string;
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/formats/yaml.d.ts
|
|
22
|
+
type ParseYAMLOptions = DocumentOptions & ParseOptions$2 & SchemaOptions & ToJSOptions;
|
|
23
|
+
declare function parseYAML(stringifiedContent: string, parseOptions?: ParseYAMLOptions): unknown;
|
|
24
|
+
type StringifyYAMLOptions = CreateNodeOptions & DocumentOptions & ParseOptions$2 & SchemaOptions & ToStringOptions;
|
|
25
|
+
declare function stringifyYAML(unknownContent: unknown, stringifyOptions?: StringifyYAMLOptions): string;
|
|
11
26
|
//#endregion
|
|
12
27
|
//#region src/utils.d.ts
|
|
13
28
|
type ErrorInfo<T> = T & {
|
|
@@ -16,40 +31,58 @@ type ErrorInfo<T> = T & {
|
|
|
16
31
|
declare class EnhancedError<T = unknown> extends Error {
|
|
17
32
|
constructor(message: string, info: ErrorInfo<T>);
|
|
18
33
|
}
|
|
34
|
+
declare function extractFormatSuffix(format: string): string;
|
|
19
35
|
//#endregion
|
|
20
36
|
//#region src/parse.d.ts
|
|
21
|
-
declare class ParseError extends EnhancedError {
|
|
22
|
-
|
|
37
|
+
declare class ParseError extends EnhancedError {
|
|
38
|
+
readonly tag = "ParseError";
|
|
39
|
+
constructor(cause: unknown);
|
|
40
|
+
}
|
|
41
|
+
type ParseOptions = Partial<{
|
|
23
42
|
json: ParseJSONOptions;
|
|
24
|
-
|
|
25
|
-
|
|
43
|
+
yaml: ParseYAMLOptions;
|
|
44
|
+
format: string;
|
|
45
|
+
}>;
|
|
26
46
|
declare const defaultParseOptions: {
|
|
27
47
|
readonly json: {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
48
|
+
allowEmptyContent: false;
|
|
49
|
+
allowTrailingComma: false;
|
|
50
|
+
disallowComments: true;
|
|
31
51
|
};
|
|
52
|
+
readonly yaml: {};
|
|
32
53
|
};
|
|
33
|
-
declare const parseIt: (stringifiedContent: string, parseOptions:
|
|
54
|
+
declare const parseIt: (stringifiedContent: string, parseOptions: Partial<{
|
|
55
|
+
json: ParseJSONOptions;
|
|
56
|
+
yaml: ParseYAMLOptions;
|
|
57
|
+
format: string;
|
|
58
|
+
}>) => neverthrow4.Result<unknown, ParseError>;
|
|
34
59
|
//#endregion
|
|
35
60
|
//#region src/schema.d.ts
|
|
36
|
-
interface StringSchema extends StandardSchemaV1<string> {
|
|
61
|
+
interface StringSchema extends StandardSchemaV1$1<string> {
|
|
37
62
|
type: "string";
|
|
38
63
|
message: string;
|
|
39
64
|
}
|
|
40
65
|
declare function parseString(message?: string): StringSchema;
|
|
41
66
|
//#endregion
|
|
42
67
|
//#region src/stringify.d.ts
|
|
43
|
-
declare class StringifyError extends EnhancedError {
|
|
44
|
-
|
|
68
|
+
declare class StringifyError extends EnhancedError {
|
|
69
|
+
readonly tag = "StringifyError";
|
|
70
|
+
constructor(cause: unknown);
|
|
71
|
+
}
|
|
72
|
+
type StringifyOptions = Partial<{
|
|
45
73
|
json: StringifyJSONOptions;
|
|
46
|
-
|
|
74
|
+
yaml: StringifyYAMLOptions;
|
|
75
|
+
}>;
|
|
47
76
|
declare const defaultStringifyOptions: {
|
|
48
77
|
readonly json: 0;
|
|
78
|
+
readonly yaml: {};
|
|
49
79
|
};
|
|
50
|
-
declare const stringifyIt: (unknownContent: unknown, formatAndStringifyOptions: Partial<
|
|
80
|
+
declare const stringifyIt: (unknownContent: unknown, formatAndStringifyOptions: Partial<Partial<{
|
|
81
|
+
json: StringifyJSONOptions;
|
|
82
|
+
yaml: StringifyYAMLOptions;
|
|
83
|
+
}> & {
|
|
51
84
|
format: string;
|
|
52
|
-
}>) =>
|
|
85
|
+
}>) => neverthrow3.Result<string, StringifyError>;
|
|
53
86
|
//#endregion
|
|
54
87
|
//#region src/typeHelpers.d.ts
|
|
55
88
|
type ObjectKeys<T extends object> = `${Exclude<keyof T, symbol>}`;
|
|
@@ -58,11 +91,17 @@ declare const objectEntries: <Type extends Record<string, unknown>>(value: Type)
|
|
|
58
91
|
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
59
92
|
//#endregion
|
|
60
93
|
//#region src/validate.d.ts
|
|
94
|
+
interface ValidationIssue extends StandardSchemaV1$1.Issue {
|
|
95
|
+
input: unknown;
|
|
96
|
+
}
|
|
97
|
+
type ValidationIssues = ReadonlyArray<ValidationIssue>;
|
|
61
98
|
declare class ValidationError extends EnhancedError {
|
|
62
|
-
readonly
|
|
63
|
-
|
|
99
|
+
readonly tag = "ValidationError";
|
|
100
|
+
readonly originalValue: unknown;
|
|
101
|
+
readonly issues: ValidationIssues;
|
|
102
|
+
constructor(originalValue: unknown, issues: ValidationIssues);
|
|
64
103
|
}
|
|
65
|
-
declare const validateIt: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema) => ResultAsync<StandardSchemaV1.InferOutput<Schema>, ValidationError>;
|
|
104
|
+
declare const validateIt: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema) => ResultAsync<StandardSchemaV1$1.InferOutput<Schema>, ValidationError>;
|
|
66
105
|
//#endregion
|
|
67
106
|
//#region src/zerde.d.ts
|
|
68
107
|
type ZerdeOptions = Partial<{
|
|
@@ -70,22 +109,21 @@ type ZerdeOptions = Partial<{
|
|
|
70
109
|
stringify: StringifyOptions;
|
|
71
110
|
}>;
|
|
72
111
|
declare class Zerde {
|
|
73
|
-
parseOptions:
|
|
74
|
-
|
|
75
|
-
readonly allowEmptyContent: true;
|
|
76
|
-
readonly allowTrailingComma: true;
|
|
77
|
-
readonly disallowComments: true;
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
stringifyOptions: {
|
|
81
|
-
readonly json: 0;
|
|
82
|
-
};
|
|
112
|
+
parseOptions: ParseOptions;
|
|
113
|
+
stringifyOptions: StringifyOptions;
|
|
83
114
|
constructor(options?: ZerdeOptions);
|
|
84
|
-
parse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string |
|
|
85
|
-
stringify: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string |
|
|
115
|
+
parse: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, parseOptions?: string | ParseOptions) => neverthrow0.ResultAsync<StandardSchemaV1$1.InferOutput<Schema>, ParseError | ValidationError>;
|
|
116
|
+
stringify: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | StringifyOptions) => neverthrow0.ResultAsync<string, ValidationError | StringifyError>;
|
|
86
117
|
}
|
|
87
118
|
declare const zerde: Zerde;
|
|
88
|
-
declare const zparse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<
|
|
89
|
-
|
|
119
|
+
declare const zparse: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<{
|
|
120
|
+
json: ParseJSONOptions;
|
|
121
|
+
yaml: ParseYAMLOptions;
|
|
122
|
+
format: string;
|
|
123
|
+
}> | undefined) => neverthrow0.ResultAsync<StandardSchemaV1$1.InferOutput<Schema>, ParseError | ValidationError>;
|
|
124
|
+
declare const zstringify: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | Partial<{
|
|
125
|
+
json: StringifyJSONOptions;
|
|
126
|
+
yaml: StringifyYAMLOptions;
|
|
127
|
+
}> | undefined) => neverthrow0.ResultAsync<string, ValidationError | StringifyError>;
|
|
90
128
|
//#endregion
|
|
91
|
-
export { EnhancedError,
|
|
129
|
+
export { EnhancedError, ErrorInfo, ParseError, ParseJSONOptions, ParseOptions, ParseYAMLOptions, Prettify, type StandardSchemaV1, StringSchema, StringifyError, StringifyJSONOptions, StringifyOptions, StringifyYAMLOptions, ValidationError, ValidationIssue, ValidationIssues, Zerde, ZerdeOptions, defaultParseJSONOptions, defaultParseOptions, defaultStringifyJSONOptions, defaultStringifyOptions, extractFormatSuffix, objectEntries, objectKeys, parseIt, parseJSON, parseString, parseYAML, stringifyIt, stringifyJSON, stringifyYAML, validateIt, zerde, zparse, zstringify };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{parse as e}from"jsonc-parser";import{configure as t}from"safe-stable-stringify";import{parse as n,stringify as r}from"yaml";import{ResultAsync as i,fromThrowable as a}from"neverthrow";const o={allowEmptyContent:!1,allowTrailingComma:!1,disallowComments:!0};function s(t,n){let r={...o,...n},i=[],a=e(t,i,r);if(i.length>0)throw SyntaxError(`Could not parse JSON`,{cause:i});return a}const c=0,l=t({bigint:!1,circularValue:TypeError,deterministic:!0,strict:!0});function u(e,t){let n=l(e,null,t);if(n===void 0)throw SyntaxError(`safe-stable-stringify should have thrown an error if trying to stringify an invalid json object`);return n}function d(e,t){return n(e,t)}function f(e,t){return r(e,{sortMapEntries:!0,...t})}const p=Object.keys,m=Object.entries;var h=class extends Error{constructor(e,t){super(e,{cause:t.cause}),this.name=this.constructor.name}};const g=[`.`,`/`,`\\`,`+`];function _(e){let t=e.trim().toLowerCase();if(!t)return t;let n=t.indexOf(`;`);n>0&&(t=t.slice(0,n));let r=Math.max(...g.map(e=>t.lastIndexOf(e)));return r<0?t:t.slice(r+1)}if(import.meta.vitest){let{describe:e,it:t,expect:n}=import.meta.vitest,r={lowercase:`json`,uppercase:`JSON`,mixedcase:`jSoN`,extension:`.json`,file:`file.json`,"file with path":`some/path/to/file.json`,"file with a windows path":`some\\path\\to\\file.json`,"basic media type":`application/json`,"complex media type":`application/customFormat+json`,"media type with charset":`application/customFormat+json; charset=utf-8`};e(`extractFormatSuffix`,()=>{for(let[e,i]of m(r)){let r=`Can handle ${e}: ${i}`;t(r,()=>{n(_(i)).toEqual(`json`)})}})}var v=class extends h{tag=`ParseError`;constructor(e){super(`Could not parse content`,{cause:e}),this.name=`ParseError`}};const y={json:o,yaml:{}},b=a((e,t)=>{let{format:n,...r}=t,i=p(r).sort().at(0)??``,a=_(n??i);return a===`yaml`||a===`yml`?d(e,r.yaml):a===`json`?s(e,r.json):e},e=>new v(e));function x(e=`Invalid type`){return{type:`string`,message:e,"~standard":{version:1,vendor:`StandardSchema`,validate(t){return typeof t==`string`?{value:t}:{issues:[{message:e}]}}}}}var S=class extends h{tag=`StringifyError`;constructor(e){super(`Could not stringify content`,{cause:e}),this.name=`StringifyError`}};const C={json:c,yaml:{}},w=a((e,t)=>{if(typeof e==`string`)return e;let{format:n,...r}=t,i=_(n??``);return i===`yaml`||i===`yml`?f(e,r.yaml):u(e,r.json)},e=>new S(e));var T=class extends h{tag=`ValidationError`;originalValue;issues;constructor(e,t){super(`Content was not valid`,{cause:void 0}),this.originalValue=e,this.issues=t,this.name=`ValidationError`}};const E=i.fromThrowable(async(e,t)=>{let n=await t[`~standard`].validate(e);if(n.issues){let t=n.issues.map(t=>({...t,input:D(e,t)}));throw new T(e,t)}return n.value},e=>e);function D(e,t){let n=e;for(let e of t.path??[])n=n[e];return n}if(import.meta.vitest){let{describe:e,it:t,expect:n}=import.meta.vitest,r=[[`simpleObject`,[`hello`],{hello:`world`},`world`],[`nestedObject`,[`deeply`,`nested`,`object`],{deeply:{nested:{object:`someValue`}}},`someValue`],[`array`,[1],[`zero`,`one`],`one`],[`noPath`,void 0,{hello:`world`},{hello:`world`}]];e(`extractOriginalInput`,()=>{for(let[e,i,a,o]of r){let r={message:`something went wrong`,path:i};t(`Can handle ${e}`,()=>{n(D(a,r)).toEqual(o)})}})}var O=class{parseOptions;stringifyOptions;constructor(e={}){this.parseOptions={...y,...e.parse},this.stringifyOptions={...C,...e.stringify}}parse=(e,t,n)=>{if(typeof e==`string`){let r=typeof n==`string`?{format:n}:n,i={...this.parseOptions,...r};return b(e,i).asyncAndThen(e=>E(e,t))}return E(e,t)};stringify=(e,t,n)=>{let r=typeof n==`string`?{format:n}:n,i={...this.stringifyOptions,...r};return E(e,t).andThen(e=>w(e,i))}};const k=new O,A=k.parse,j=k.stringify;export{h as EnhancedError,v as ParseError,S as StringifyError,T as ValidationError,O as Zerde,o as defaultParseJSONOptions,y as defaultParseOptions,c as defaultStringifyJSONOptions,C as defaultStringifyOptions,_ as extractFormatSuffix,m as objectEntries,p as objectKeys,b as parseIt,s as parseJSON,x as parseString,d as parseYAML,w as stringifyIt,u as stringifyJSON,f as stringifyYAML,E as validateIt,k as zerde,A as zparse,j as zstringify};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zerde",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "parsing, and stringifying combined with schema validation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"parse",
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
"@standard-schema/spec": "^1.0.0",
|
|
48
48
|
"jsonc-parser": "^3.3.1",
|
|
49
49
|
"neverthrow": "^8.2.0",
|
|
50
|
-
"safe-stable-stringify": "^2.5.0"
|
|
50
|
+
"safe-stable-stringify": "^2.5.0",
|
|
51
|
+
"yaml": "^2.8.2"
|
|
51
52
|
}
|
|
52
53
|
}
|