schematch 0.0.1
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/LICENSE +190 -0
- package/README.md +526 -0
- package/dist/errors.d.ts +28 -0
- package/dist/errors.js +149 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/is-matching.d.ts +6 -0
- package/dist/is-matching.js +28 -0
- package/dist/is-matching.js.map +1 -0
- package/dist/match.d.ts +221 -0
- package/dist/match.js +616 -0
- package/dist/match.js.map +1 -0
- package/dist/standard-schema/compiled.d.ts +21 -0
- package/dist/standard-schema/compiled.js +578 -0
- package/dist/standard-schema/compiled.js.map +1 -0
- package/dist/standard-schema/contract.d.ts +55 -0
- package/dist/standard-schema/contract.js +3 -0
- package/dist/standard-schema/contract.js.map +1 -0
- package/dist/standard-schema/errors.d.ts +7 -0
- package/dist/standard-schema/errors.js +54 -0
- package/dist/standard-schema/errors.js.map +1 -0
- package/dist/standard-schema/utils.d.ts +3 -0
- package/dist/standard-schema/utils.js +10 -0
- package/dist/standard-schema/utils.js.map +1 -0
- package/dist/standard-schema/validation.d.ts +6 -0
- package/dist/standard-schema/validation.js +23 -0
- package/dist/standard-schema/validation.js.map +1 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract.js","sourceRoot":"","sources":["../../src/standard-schema/contract.ts"],"names":[],"mappings":"AAAA,0DAA0D"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from './contract.js';
|
|
2
|
+
export declare const prettifyStandardSchemaError: (error: unknown) => string | null;
|
|
3
|
+
export declare function toDotPath(path: (string | number | symbol)[]): string;
|
|
4
|
+
export declare class StandardSchemaV1Error extends Error implements StandardSchemaV1.FailureResult {
|
|
5
|
+
issues: StandardSchemaV1.FailureResult['issues'];
|
|
6
|
+
constructor(failure: StandardSchemaV1.FailureResult);
|
|
7
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { looksLikeStandardSchemaFailure } from './utils.js';
|
|
2
|
+
export const prettifyStandardSchemaError = (error) => {
|
|
3
|
+
if (!looksLikeStandardSchemaFailure(error))
|
|
4
|
+
return null;
|
|
5
|
+
const issues = [...error.issues]
|
|
6
|
+
.map(issue => {
|
|
7
|
+
const path = issue.path || [];
|
|
8
|
+
const primitivePathSegments = path.map(segment => {
|
|
9
|
+
if (typeof segment === 'string' || typeof segment === 'number' || typeof segment === 'symbol')
|
|
10
|
+
return segment;
|
|
11
|
+
return segment.key;
|
|
12
|
+
});
|
|
13
|
+
const dotPath = toDotPath(primitivePathSegments);
|
|
14
|
+
return {
|
|
15
|
+
issue,
|
|
16
|
+
path,
|
|
17
|
+
primitivePathSegments,
|
|
18
|
+
dotPath,
|
|
19
|
+
};
|
|
20
|
+
})
|
|
21
|
+
.sort((a, b) => a.path.length - b.path.length);
|
|
22
|
+
const lines = [];
|
|
23
|
+
for (const { issue, dotPath } of issues) {
|
|
24
|
+
let message = `✖ ${issue.message}`;
|
|
25
|
+
if (dotPath)
|
|
26
|
+
message += ` → at ${dotPath}`;
|
|
27
|
+
lines.push(message);
|
|
28
|
+
}
|
|
29
|
+
return lines.join('\n');
|
|
30
|
+
};
|
|
31
|
+
export function toDotPath(path) {
|
|
32
|
+
const segs = [];
|
|
33
|
+
for (const seg of path) {
|
|
34
|
+
if (typeof seg === 'number')
|
|
35
|
+
segs.push(`[${seg}]`);
|
|
36
|
+
else if (typeof seg === 'symbol')
|
|
37
|
+
segs.push(`[${JSON.stringify(String(seg))}]`);
|
|
38
|
+
else if (/[^\w$]/.test(seg))
|
|
39
|
+
segs.push(`[${JSON.stringify(seg)}]`);
|
|
40
|
+
else {
|
|
41
|
+
if (segs.length)
|
|
42
|
+
segs.push('.');
|
|
43
|
+
segs.push(seg);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return segs.join('');
|
|
47
|
+
}
|
|
48
|
+
export class StandardSchemaV1Error extends Error {
|
|
49
|
+
constructor(failure) {
|
|
50
|
+
super('Standard Schema error - details in `issues`.');
|
|
51
|
+
this.issues = failure.issues;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/standard-schema/errors.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,8BAA8B,EAAC,MAAM,YAAY,CAAA;AAEzD,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAAC,KAAc,EAAiB,EAAE;IAC3E,IAAI,CAAC,8BAA8B,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAA;IAEvD,MAAM,MAAM,GAAG,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC;SAC7B,GAAG,CAAC,KAAK,CAAC,EAAE;QACX,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,IAAI,EAAE,CAAA;QAC7B,MAAM,qBAAqB,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;YAC/C,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,OAAO,KAAK,QAAQ;gBAAE,OAAO,OAAO,CAAA;YAC7G,OAAO,OAAO,CAAC,GAAG,CAAA;QACpB,CAAC,CAAC,CAAA;QACF,MAAM,OAAO,GAAG,SAAS,CAAC,qBAAqB,CAAC,CAAA;QAChD,OAAO;YACL,KAAK;YACL,IAAI;YACJ,qBAAqB;YACrB,OAAO;SACR,CAAA;IACH,CAAC,CAAC;SACD,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA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O,CAAC,MAAM,CAAA;IAC9B,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const looksLikeStandardSchemaFailure = (error) => {
|
|
2
|
+
return !!error && typeof error === 'object' && 'issues' in error && Array.isArray(error.issues);
|
|
3
|
+
};
|
|
4
|
+
export const looksLikeStandardSchema = (thing) => {
|
|
5
|
+
return (!!thing &&
|
|
6
|
+
(typeof thing === 'object' || typeof thing === 'function') &&
|
|
7
|
+
'~standard' in thing &&
|
|
8
|
+
typeof thing['~standard'] === 'object');
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/standard-schema/utils.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAAC,KAAc,EAA2C,EAAE;IACxG,OAAO,CAAC,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,QAAQ,IAAI,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;AACjG,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,KAAc,EAA6B,EAAE;IACnF,OAAO,CACL,CAAC,CAAC,KAAK;QACP,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,UAAU,CAAC;QAC1D,WAAW,IAAI,KAAK;QACpB,OAAO,KAAK,CAAC,WAAW,CAAC,KAAK,QAAQ,CACvC,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from './contract.js';
|
|
2
|
+
export declare function assertStandardSchema(schema: unknown): asserts schema is StandardSchemaV1;
|
|
3
|
+
export declare const isPromiseLike: (value: unknown) => value is Promise<unknown>;
|
|
4
|
+
export declare const validateSync: <Output>(schema: StandardSchemaV1<unknown, Output>, value: unknown) => StandardSchemaV1.Result<Output>;
|
|
5
|
+
export declare const validateAsync: <Output>(schema: StandardSchemaV1<unknown, Output>, value: unknown) => Promise<StandardSchemaV1.Result<Output>>;
|
|
6
|
+
export declare const isSuccess: <Output>(result: StandardSchemaV1.Result<Output>) => result is StandardSchemaV1.SuccessResult<Output>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { looksLikeStandardSchema, looksLikeStandardSchemaFailure } from './utils.js';
|
|
2
|
+
export function assertStandardSchema(schema) {
|
|
3
|
+
if (!looksLikeStandardSchema(schema)) {
|
|
4
|
+
throw new TypeError('Expected a Standard Schema value with a `~standard` property.');
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export const isPromiseLike = (value) => {
|
|
8
|
+
return !!value && (typeof value === 'object' || typeof value === 'function') && 'then' in value;
|
|
9
|
+
};
|
|
10
|
+
export const validateSync = (schema, value) => {
|
|
11
|
+
const result = schema['~standard'].validate(value);
|
|
12
|
+
if (isPromiseLike(result)) {
|
|
13
|
+
throw new Error('Schema validation returned a Promise. Use matchAsync or isMatchingAsync instead.');
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
};
|
|
17
|
+
export const validateAsync = async (schema, value) => {
|
|
18
|
+
return await schema['~standard'].validate(value);
|
|
19
|
+
};
|
|
20
|
+
export const isSuccess = (result) => {
|
|
21
|
+
return !looksLikeStandardSchemaFailure(result);
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/standard-schema/validation.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,uBAAuB,EAAE,8BAA8B,EAAC,MAAM,YAAY,CAAA;AAElF,MAAM,UAAU,oBAAoB,CAAC,MAAe;IAClD,IAAI,CAAC,uBAAuB,CAAC,MAAM,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,SAAS,CAAC,+DAA+D,CAAC,CAAA;IACtF,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAc,EAA6B,EAAE;IACzE,OAAO,CAAC,CAAC,KAAK,IAAI,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,UAAU,CAAC,IAAI,MAAM,IAAI,KAAK,CAAA;AACjG,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,MAAyC,EACzC,KAAc,EACmB,EAAE;IACnC,MAAM,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;IAClD,IAAI,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,kFAAkF,CAAC,CAAA;IACrG,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAChC,MAAyC,EACzC,KAAc,EAC4B,EAAE;IAC5C,OAAO,MAAM,MAAM,CAAC,WAAW,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;AAClD,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,MAAuC,EACW,EAAE;IACpD,OAAO,CAAC,8BAA8B,CAAC,MAAM,CAAC,CAAA;AAChD,CAAC,CAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from './standard-schema/contract.js';
|
|
2
|
+
export type StandardSchema = StandardSchemaV1;
|
|
3
|
+
export type InferInput<Schema extends StandardSchemaV1> = StandardSchemaV1.InferInput<Schema>;
|
|
4
|
+
export type InferOutput<Schema extends StandardSchemaV1> = StandardSchemaV1.InferOutput<Schema>;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "schematch",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Schema-first pattern matching for TypeScript — use zod, valibot, arktype, or any Standard Schema validator as match/case clauses",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"default": "./dist/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
18
|
+
"prepare": "pnpm build",
|
|
19
|
+
"attw": "attw --pack . --ignore-rules cjs-resolves-to-esm no-resolution",
|
|
20
|
+
"test": "vitest run --typecheck",
|
|
21
|
+
"test:watch": "vitest --typecheck"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"pattern-matching",
|
|
25
|
+
"match",
|
|
26
|
+
"schema",
|
|
27
|
+
"standard-schema",
|
|
28
|
+
"zod",
|
|
29
|
+
"valibot",
|
|
30
|
+
"arktype",
|
|
31
|
+
"validation",
|
|
32
|
+
"type-guard",
|
|
33
|
+
"typescript",
|
|
34
|
+
"type-inference",
|
|
35
|
+
"discriminated-union",
|
|
36
|
+
"exhaustive"
|
|
37
|
+
],
|
|
38
|
+
"author": "mmkal",
|
|
39
|
+
"license": "Apache-2.0",
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/mmkal/schematch.git"
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/mmkal/schematch/issues"
|
|
46
|
+
},
|
|
47
|
+
"packageManager": "pnpm@10.28.1",
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@arethetypeswrong/cli": "0.18.2",
|
|
50
|
+
"@types/node": "25.2.2",
|
|
51
|
+
"arktype": "2.1.29",
|
|
52
|
+
"effect": "3.19.16",
|
|
53
|
+
"ts-pattern": "5.9.0",
|
|
54
|
+
"typescript": "5.7.3",
|
|
55
|
+
"valibot": "1.2.0",
|
|
56
|
+
"vitest": "4.0.18",
|
|
57
|
+
"zod": "4.3.6"
|
|
58
|
+
}
|
|
59
|
+
}
|