zerde 0.1.0

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/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # Zerde
2
+
3
+ Typescript library for parsing and stringifying combined with schema validationg
4
+
5
+ When stringifying or parsing an object, you want to verify that what you are stringifying or parsing matches a schema.
6
+ This is the overall workflow:
7
+ Parsing: stringifiedContent -> parse -> SomeObject
8
+ Stringifying: SomeObject -> stringify -> stringifiedContent
9
+
10
+ However, all type safety is lost during these steps (especially during the parse step)
11
+
12
+ The desired workflow:
13
+
14
+ Parsing: stringifiedContent<SomeType> -> parse -> Unknown -> validateIsSomeType -> SomeType
15
+ Stringifying: SomeType -> validateIsSomeType -> stringify -> stringifiedContent<SomeType>
16
+
17
+ This way, you are able to parse some unknown string into a strongly typed result, and ensure that the object you are about to stringify matches the intended schema
18
+
19
+ The next step is each `parse`/`stringify` function is specific to one format. `JSON.parse`/`JSON.stringify` only handles `JSON`. What if you need to parse/stringify some other format? Import another library.
20
+
21
+ This library provides a unified way to parse, stringify and validate:
22
+
23
+ [x] JSON
24
+ [] YAML (coming soon)
25
+ [] TOML (coming soon)
26
+ [] CSV (coming soon)
27
+
28
+ Makes use of [Standard Schema](https://github.com/standard-schema/standard-schema) to support all the popular schema libraries (Zod, Valibot, etc).
29
+
30
+ ## Quick Start
31
+
32
+ ```ts
33
+ import { zparse, zstringify } from "zerde"
34
+ import { z } from "zod"
35
+
36
+ const personSchema = z.object({
37
+ name: z.string(),
38
+ age: z.number()
39
+ })
40
+
41
+ const aPerson = {
42
+ name: "Some Name",
43
+ age: 30
44
+ }
45
+
46
+ // Will check if aPerson matches the personSchema, then stringify the person
47
+ const stringifyResult = await zstringify(aPerson, personSchema)
48
+ // ^stringifyResult is a neverthrow ResultAsync
49
+
50
+ if (stringifyResult.isOk()) {
51
+ const stringifiedPerson = stringifyResult.data
52
+ // ^stringifiedPerson is '{"name":"Some Name","age":30}'
53
+
54
+ // Will parse the stringifiedPerson, then check if the output matches the personSchema
55
+ const parseResult = await zparse(stringifiedPerson, personSchema, "JSON")
56
+ // ^parseResult is a neverthrow ResultAsync
57
+
58
+ if (parseResult.isOk()) {
59
+ // This is the same aPerson from before
60
+ const parsedPerson = parseResult.data
61
+
62
+ assert(aPerson.name === parsedPerson.name)
63
+ assert(aPerson.age === parsedPerson.age)
64
+
65
+ } else {
66
+ // this error could have been from parsing, or from validating agains the schema
67
+ const parseError = parseResult.error
68
+ }
69
+
70
+ } else {
71
+ // This error could have been from stringifying, or from validating against the schema
72
+ const stringifyError = stringifyResult.error
73
+ }
74
+ ```
75
+
76
+ ## Specifying a Format
77
+
78
+ Can provide a format as a string:
79
+
80
+ ```ts
81
+ zparse(someJSONstringifiedString, someSchema, "JSON")
82
+ ```
83
+
84
+ Handles a wide variety of string cases:
85
+
86
+ | Case | Example |
87
+ | --- | --- |
88
+ | uppercase | "JSON" |
89
+ | lowercase | "json" |
90
+ | mixedcase | "jSoN" |
91
+ | extension | ".json" |
92
+ | file | "file.json" |
93
+ | file with path | "some/path/to/file.json" |
94
+ | file with a windows path | "some\\path\\to\\file.json" |
95
+ | basic media type | "application/json" |
96
+ | complex media type | "application/customFormat+json" |
97
+ | media type with charset | "application/customFormat+json; charset=utf-8" |
98
+
99
+
100
+ When calling `zparse`, if the unknownContent was not a string, or if no format is specified, or if the specified format is not supported, `zparse` will not modify the passed in content before validating it. So if you know you are parsing a `JSON.stringify`-ed string, pass `JSON` as the 3rd arg to `zparse`
101
+
102
+ When calling `zstringify`, if no format is specified, or if the specified format is not supported, `zparse` fall back to assuming the format was `JSON`.
103
+
@@ -0,0 +1,85 @@
1
+ import * as neverthrow0$1 from "neverthrow";
2
+ import * as neverthrow4 from "neverthrow";
3
+ import * as neverthrow0 from "neverthrow";
4
+ import { ResultAsync } from "neverthrow";
5
+ import { ParseOptions as ParseOptions$1 } from "jsonc-parser";
6
+ import { StandardSchemaV1 } from "@standard-schema/spec";
7
+
8
+ //#region src/formats/json.d.ts
9
+ type ParseJSONOptions = Partial<ParseOptions$1>;
10
+ type StringifyJSONOptions = string | number;
11
+ //#endregion
12
+ //#region src/utils.d.ts
13
+ type ErrorInfo<T> = T & {
14
+ cause: unknown;
15
+ };
16
+ declare class EnhancedError<T = unknown> extends Error {
17
+ constructor(message: string, info: ErrorInfo<T>);
18
+ }
19
+ //#endregion
20
+ //#region src/parse.d.ts
21
+ declare class ParseError extends EnhancedError {}
22
+ type ParseOptions = {
23
+ json: ParseJSONOptions;
24
+ };
25
+ declare const defaultParseOptions: {
26
+ readonly json: {
27
+ readonly allowEmptyContent: true;
28
+ readonly allowTrailingComma: true;
29
+ readonly disallowComments: true;
30
+ };
31
+ };
32
+ declare const parseIt: (stringifiedContent: string, formatAndParseOptions: Partial<ParseOptions & {
33
+ format: string;
34
+ }>) => neverthrow0$1.Result<unknown, ParseError>;
35
+ //#endregion
36
+ //#region src/stringify.d.ts
37
+ declare class StringifyError extends EnhancedError {}
38
+ type StringifyOptions = {
39
+ json: StringifyJSONOptions;
40
+ };
41
+ declare const defaultStringifyOptions: {
42
+ readonly json: 0;
43
+ };
44
+ declare const stringifyIt: (unknownContent: unknown, formatAndStringifyOptions: Partial<StringifyOptions & {
45
+ format: string;
46
+ }>) => neverthrow4.Result<string, StringifyError>;
47
+ //#endregion
48
+ //#region src/typeHelpers.d.ts
49
+ type ObjectKeys<T extends object> = `${Exclude<keyof T, symbol>}`;
50
+ declare const objectKeys: <Type extends Record<string, unknown>>(value: Type) => Array<ObjectKeys<Type>>;
51
+ declare const objectEntries: <Type extends Record<string, unknown>>(value: Type) => Array<[ObjectKeys<Type>, Required<Type>[ObjectKeys<Type>]]>;
52
+ type Prettify<T> = { [K in keyof T]: T[K] } & {};
53
+ //#endregion
54
+ //#region src/validate.d.ts
55
+ declare class ValidationError extends EnhancedError {
56
+ readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
57
+ constructor(issues: ReadonlyArray<StandardSchemaV1.Issue>);
58
+ }
59
+ declare const validateIt: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema) => ResultAsync<unknown, ValidationError>;
60
+ //#endregion
61
+ //#region src/zerde.d.ts
62
+ type ZerdeOptions = Partial<{
63
+ parse: ParseOptions;
64
+ stringify: StringifyOptions;
65
+ }>;
66
+ declare class Zerde {
67
+ parseOptions: {
68
+ readonly json: {
69
+ readonly allowEmptyContent: true;
70
+ readonly allowTrailingComma: true;
71
+ readonly disallowComments: true;
72
+ };
73
+ };
74
+ stringifyOptions: {
75
+ readonly json: 0;
76
+ };
77
+ constructor(options?: ZerdeOptions);
78
+ parse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<ParseOptions>) => neverthrow0.ResultAsync<unknown, ParseError | ValidationError>;
79
+ stringify: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | Partial<StringifyOptions>) => neverthrow0.ResultAsync<string, ValidationError | StringifyError>;
80
+ }
81
+ declare const zerde: Zerde;
82
+ declare const zparse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<ParseOptions>) => neverthrow0.ResultAsync<unknown, ParseError | ValidationError>;
83
+ declare const zstringify: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | Partial<StringifyOptions>) => neverthrow0.ResultAsync<string, ValidationError | StringifyError>;
84
+ //#endregion
85
+ export { EnhancedError, type ErrorInfo, ParseError, type ParseOptions, Prettify, StringifyError, type StringifyOptions, ValidationError, Zerde, type ZerdeOptions, defaultParseOptions, defaultStringifyOptions, objectEntries, objectKeys, parseIt, stringifyIt, validateIt, zerde, zparse, zstringify };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ import{ResultAsync as e,fromThrowable as t}from"neverthrow";import{parse as n}from"jsonc-parser";import{configure as r}from"safe-stable-stringify";const i={allowEmptyContent:!0,allowTrailingComma:!0,disallowComments:!0};function a(e,t={}){let r={...t,...i},a=[],o=n(e,a,r);if(a.length>0)throw SyntaxError(`Could not parse JSON`,{cause:a});return o}const o=0,s=r({bigint:!1,circularValue:SyntaxError,deterministic:!0,strict:!0});function c(e,t){let n=s(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}const l=Object.keys,u=Object.entries;var d=class extends Error{constructor(e,t){super(e,{cause:t.cause}),this.name=this.constructor.name}};const f=[`.`,`/`,`\\`,`+`];function p(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(...f.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 u(r)){let r=`Can handle ${e}: ${i}`;t(r,()=>{n(p(i)).toEqual(`json`)})}})}var m=class extends d{};const h={json:i},g=t((e,t)=>{let{format:n,...r}=t,i=p(n??``);return i===`json`?a(e,r.json):e},e=>new m(`Could not parse content`,{cause:e}));var _=class extends d{};const v={json:o},y=t((e,t)=>{if(typeof e==`string`)return e;let{format:n,...r}=t,i=p(n??``);return c(e,r.json)},e=>new _(`Could not stringify content`,{cause:e}));var b=class extends d{issues;constructor(e){super(`Content was not valid`,{cause:e}),this.issues=e}};const x=e.fromThrowable(async(e,t)=>{let n=await t[`~standard`].validate(e);if(n.issues)throw new b(n.issues);return n.value},e=>e);var S=class{parseOptions=h;stringifyOptions=v;constructor(e={}){this.parseOptions={...e.parse,...h}}parse=(e,t,n)=>{if(typeof e==`string`){let r=typeof n==`string`?{format:n}:n,i={...this.parseOptions,...r};return g(e,i).asyncAndThen(e=>x(e,t))}return x(e,t)};stringify=(e,t,n)=>{let r=typeof n==`string`?{format:n}:n,i={...this.stringifyOptions,...r};return x(e,t).andThen(e=>y(e,i))}};const C=new S,w=C.parse,T=C.stringify;export{d as EnhancedError,m as ParseError,_ as StringifyError,b as ValidationError,S as Zerde,h as defaultParseOptions,v as defaultStringifyOptions,u as objectEntries,l as objectKeys,g as parseIt,y as stringifyIt,x as validateIt,C as zerde,w as zparse,T as zstringify};
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "zerde",
3
+ "version": "0.1.0",
4
+ "description": "parsing, and stringifying combined with schema validation",
5
+ "keywords": [
6
+ "parse",
7
+ "stringify",
8
+ "validate",
9
+ "validation",
10
+ "schema"
11
+ ],
12
+ "repository": "github:dannywexler/zerde",
13
+ "author": "Danny Wexler",
14
+ "license": "MIT",
15
+ "type": "module",
16
+ "engines": {
17
+ "node": ">=20.0.0"
18
+ },
19
+ "exports": {
20
+ ".": {
21
+ "import": "./dist/index.js",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "main": "./dist/index.js",
26
+ "module": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "devDependencies": {
32
+ "@biomejs/biome": "2.2.6",
33
+ "lefthook": "^1.13.6",
34
+ "tsdown": "^0.12.9",
35
+ "typescript": "^5.9.3",
36
+ "vite-tsconfig-paths": "^5.1.4",
37
+ "vitest": "^3.2.4"
38
+ },
39
+ "scripts": {
40
+ "build": "lefthook run build",
41
+ "lint": "biome check --error-on-warnings --colors=force --write src",
42
+ "test": "vitest run",
43
+ "test:watch": "vitest"
44
+ },
45
+ "dependencies": {
46
+ "@standard-schema/spec": "^1.0.0",
47
+ "jsonc-parser": "^3.3.1",
48
+ "neverthrow": "^8.2.0",
49
+ "safe-stable-stringify": "^2.5.0"
50
+ }
51
+ }