zerde 0.1.0 → 0.1.2

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 CHANGED
@@ -3,27 +3,31 @@
3
3
  Typescript library for parsing and stringifying combined with schema validationg
4
4
 
5
5
  When stringifying or parsing an object, you want to verify that what you are stringifying or parsing matches a schema.
6
+
6
7
  This is the overall workflow:
8
+
7
9
  Parsing: stringifiedContent -> parse -> SomeObject
10
+
8
11
  Stringifying: SomeObject -> stringify -> stringifiedContent
9
12
 
10
13
  However, all type safety is lost during these steps (especially during the parse step)
11
14
 
12
15
  The desired workflow:
13
16
 
14
- Parsing: stringifiedContent<SomeType> -> parse -> Unknown -> validateIsSomeType -> SomeType
15
- Stringifying: SomeType -> validateIsSomeType -> stringify -> stringifiedContent<SomeType>
17
+ Parsing: stringifiedContent\<SomeType\> -> parse -> Unknown -> validateIsSomeType -> SomeType
18
+
19
+ Stringifying: SomeType -> validateIsSomeType -> stringify -> stringifiedContent\<SomeType\>
16
20
 
17
21
  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
22
 
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.
23
+ However, 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
24
 
21
25
  This library provides a unified way to parse, stringify and validate:
22
26
 
23
- [x] JSON
24
- [] YAML (coming soon)
25
- [] TOML (coming soon)
26
- [] CSV (coming soon)
27
+ - [x] JSON
28
+ - [] YAML (coming soon)
29
+ - [] TOML (coming soon)
30
+ - [] CSV (coming soon)
27
31
 
28
32
  Makes use of [Standard Schema](https://github.com/standard-schema/standard-schema) to support all the popular schema libraries (Zod, Valibot, etc).
29
33
 
@@ -97,7 +101,7 @@ Handles a wide variety of string cases:
97
101
  | media type with charset | "application/customFormat+json; charset=utf-8" |
98
102
 
99
103
 
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`
104
+ 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
105
 
102
106
  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
107
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as neverthrow0$1 from "neverthrow";
2
- import * as neverthrow4 from "neverthrow";
3
2
  import * as neverthrow0 from "neverthrow";
3
+ import * as neverthrow1 from "neverthrow";
4
4
  import { ResultAsync } from "neverthrow";
5
5
  import { ParseOptions as ParseOptions$1 } from "jsonc-parser";
6
6
  import { StandardSchemaV1 } from "@standard-schema/spec";
@@ -21,6 +21,7 @@ declare class EnhancedError<T = unknown> extends Error {
21
21
  declare class ParseError extends EnhancedError {}
22
22
  type ParseOptions = {
23
23
  json: ParseJSONOptions;
24
+ format?: string;
24
25
  };
25
26
  declare const defaultParseOptions: {
26
27
  readonly json: {
@@ -29,9 +30,14 @@ declare const defaultParseOptions: {
29
30
  readonly disallowComments: true;
30
31
  };
31
32
  };
32
- declare const parseIt: (stringifiedContent: string, formatAndParseOptions: Partial<ParseOptions & {
33
- format: string;
34
- }>) => neverthrow0$1.Result<unknown, ParseError>;
33
+ declare const parseIt: (stringifiedContent: string, parseOptions: ParseOptions) => neverthrow0$1.Result<unknown, ParseError>;
34
+ //#endregion
35
+ //#region src/schema.d.ts
36
+ interface StringSchema extends StandardSchemaV1<string> {
37
+ type: "string";
38
+ message: string;
39
+ }
40
+ declare function parseString(message?: string): StringSchema;
35
41
  //#endregion
36
42
  //#region src/stringify.d.ts
37
43
  declare class StringifyError extends EnhancedError {}
@@ -43,7 +49,7 @@ declare const defaultStringifyOptions: {
43
49
  };
44
50
  declare const stringifyIt: (unknownContent: unknown, formatAndStringifyOptions: Partial<StringifyOptions & {
45
51
  format: string;
46
- }>) => neverthrow4.Result<string, StringifyError>;
52
+ }>) => neverthrow0.Result<string, StringifyError>;
47
53
  //#endregion
48
54
  //#region src/typeHelpers.d.ts
49
55
  type ObjectKeys<T extends object> = `${Exclude<keyof T, symbol>}`;
@@ -56,7 +62,7 @@ declare class ValidationError extends EnhancedError {
56
62
  readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
57
63
  constructor(issues: ReadonlyArray<StandardSchemaV1.Issue>);
58
64
  }
59
- declare const validateIt: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema) => ResultAsync<unknown, ValidationError>;
65
+ declare const validateIt: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema) => ResultAsync<StandardSchemaV1.InferOutput<Schema>, ValidationError>;
60
66
  //#endregion
61
67
  //#region src/zerde.d.ts
62
68
  type ZerdeOptions = Partial<{
@@ -75,11 +81,11 @@ declare class Zerde {
75
81
  readonly json: 0;
76
82
  };
77
83
  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>;
84
+ parse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<ParseOptions>) => neverthrow1.ResultAsync<StandardSchemaV1.InferOutput<Schema>, ParseError | ValidationError>;
85
+ stringify: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | Partial<StringifyOptions>) => neverthrow1.ResultAsync<string, StringifyError | ValidationError>;
80
86
  }
81
87
  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>;
88
+ declare const zparse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<ParseOptions>) => neverthrow1.ResultAsync<StandardSchemaV1.InferOutput<Schema>, ParseError | ValidationError>;
89
+ declare const zstringify: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | Partial<StringifyOptions>) => neverthrow1.ResultAsync<string, StringifyError | ValidationError>;
84
90
  //#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 };
91
+ export { EnhancedError, type ErrorInfo, ParseError, type ParseOptions, Prettify, type StringSchema, StringifyError, type StringifyOptions, ValidationError, Zerde, type ZerdeOptions, defaultParseOptions, defaultStringifyOptions, objectEntries, objectKeys, parseIt, parseString, stringifyIt, validateIt, zerde, zparse, zstringify };
package/dist/index.js CHANGED
@@ -1 +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};
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=l(r).sort().at(0)??``,o=p(n??i);return o===`json`?a(e,r.json):e},e=>new m(`Could not parse content`,{cause:e}));function _(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 v=class extends d{};const y={json:o},b=t((e,t)=>{if(typeof e==`string`)return e;let{format:n,...r}=t,i=p(n??``);return c(e,r.json)},e=>new v(`Could not stringify content`,{cause:e}));var x=class extends d{issues;constructor(e){super(`Content was not valid`,{cause:e}),this.issues=e}};const S=e.fromThrowable(async(e,t)=>{let n=await t[`~standard`].validate(e);if(n.issues)throw new x(n.issues);return n.value},e=>e);var C=class{parseOptions=h;stringifyOptions=y;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=>S(e,t))}return S(e,t)};stringify=(e,t,n)=>{let r=typeof n==`string`?{format:n}:n,i={...this.stringifyOptions,...r};return S(e,t).andThen(e=>b(e,i))}};const w=new C,T=w.parse,E=w.stringify;export{d as EnhancedError,m as ParseError,v as StringifyError,x as ValidationError,C as Zerde,h as defaultParseOptions,y as defaultStringifyOptions,u as objectEntries,l as objectKeys,g as parseIt,_ as parseString,b as stringifyIt,S as validateIt,w as zerde,T as zparse,E as zstringify};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zerde",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "parsing, and stringifying combined with schema validation",
5
5
  "keywords": [
6
6
  "parse",
@@ -9,7 +9,10 @@
9
9
  "validation",
10
10
  "schema"
11
11
  ],
12
- "repository": "github:dannywexler/zerde",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/dannywexler/zerde.git"
15
+ },
13
16
  "author": "Danny Wexler",
14
17
  "license": "MIT",
15
18
  "type": "module",
@@ -38,8 +41,6 @@
38
41
  },
39
42
  "scripts": {
40
43
  "build": "lefthook run build",
41
- "lint": "biome check --error-on-warnings --colors=force --write src",
42
- "test": "vitest run",
43
44
  "test:watch": "vitest"
44
45
  },
45
46
  "dependencies": {