@manassandhu/mint 0.0.0-stage → 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.
@@ -0,0 +1,187 @@
1
+ /**
2
+ * Mint - Tuple Schema
3
+ *
4
+ * Validates arrays with position-specific schemas.
5
+ * Supports fixed tuples and optional rest schemas.
6
+ */
7
+
8
+ import { Schema } from "../core/Schema.js";
9
+
10
+ export class TupleSchema extends Schema {
11
+ constructor(schemas, restSchema = null) {
12
+ validateSchemas(schemas);
13
+
14
+ super({
15
+ type: "tuple",
16
+ items: Object.freeze([...schemas]),
17
+ rest: restSchema,
18
+ });
19
+
20
+ this.items = Object.freeze([...schemas]);
21
+ this.restSchema = restSchema;
22
+ }
23
+
24
+ _parse(context, value) {
25
+ if (!Array.isArray(value)) {
26
+ context.addIssue({
27
+ code: "invalid_type",
28
+ expected: "array",
29
+ received: getType(value),
30
+ message: `Expected array, received ${getType(value)}`,
31
+ });
32
+
33
+ return value;
34
+ }
35
+
36
+ const minimumLength = this.items.length;
37
+
38
+ if (value.length < minimumLength) {
39
+ context.addIssue({
40
+ code: "too_small",
41
+ minimum: minimumLength,
42
+ inclusive: true,
43
+ type: "array",
44
+ message: `Array must contain at least ${minimumLength} element(s)`,
45
+ });
46
+ }
47
+
48
+ if (!this.restSchema && value.length > this.items.length) {
49
+ context.addIssue({
50
+ code: "too_big",
51
+ maximum: this.items.length,
52
+ inclusive: true,
53
+ type: "array",
54
+ message: `Array must contain at most ${this.items.length} element(s)`,
55
+ });
56
+ }
57
+
58
+ const output = [];
59
+
60
+ const length = this.restSchema
61
+ ? value.length
62
+ : Math.min(value.length, this.items.length);
63
+
64
+ for (let index = 0; index < length; index++) {
65
+ const schema =
66
+ index < this.items.length ? this.items[index] : this.restSchema;
67
+
68
+ if (!schema) {
69
+ output.push(value[index]);
70
+ continue;
71
+ }
72
+
73
+ const childContext = context.child(index, value[index]);
74
+
75
+ const parsedValue = schema._parse(childContext, value[index]);
76
+
77
+ if (isPromise(parsedValue)) {
78
+ throw new Error(
79
+ "Asynchronous tuple validation requires async parsing.",
80
+ );
81
+ }
82
+
83
+ output.push(parsedValue);
84
+ }
85
+
86
+ return output;
87
+ }
88
+
89
+ async _parseAsync(context, value) {
90
+ if (!Array.isArray(value)) {
91
+ context.addIssue({
92
+ code: "invalid_type",
93
+ expected: "array",
94
+ received: getType(value),
95
+ message: `Expected array, received ${getType(value)}`,
96
+ });
97
+
98
+ return value;
99
+ }
100
+
101
+ const minimumLength = this.items.length;
102
+
103
+ if (value.length < minimumLength) {
104
+ context.addIssue({
105
+ code: "too_small",
106
+ minimum: minimumLength,
107
+ inclusive: true,
108
+ type: "array",
109
+ message: `Array must contain at least ${minimumLength} element(s)`,
110
+ });
111
+ }
112
+
113
+ if (!this.restSchema && value.length > this.items.length) {
114
+ context.addIssue({
115
+ code: "too_big",
116
+ maximum: this.items.length,
117
+ inclusive: true,
118
+ type: "array",
119
+ message: `Array must contain at most ${this.items.length} element(s)`,
120
+ });
121
+ }
122
+
123
+ const output = [];
124
+
125
+ const length = this.restSchema
126
+ ? value.length
127
+ : Math.min(value.length, this.items.length);
128
+
129
+ for (let index = 0; index < length; index++) {
130
+ const schema =
131
+ index < this.items.length ? this.items[index] : this.restSchema;
132
+
133
+ if (!schema) {
134
+ output.push(value[index]);
135
+ continue;
136
+ }
137
+
138
+ const childContext = context.child(index, value[index]);
139
+
140
+ const parsedValue = await schema._parseAsync(childContext, value[index]);
141
+
142
+ output.push(parsedValue);
143
+ }
144
+
145
+ return output;
146
+ }
147
+
148
+ /**
149
+ * Validate additional tuple elements with a schema.
150
+ */
151
+ rest(schema) {
152
+ validateSchema(schema);
153
+
154
+ return new TupleSchema(this.items, schema);
155
+ }
156
+ }
157
+
158
+ function validateSchemas(schemas) {
159
+ if (!Array.isArray(schemas)) {
160
+ throw new TypeError("TupleSchema expects an array of schemas.");
161
+ }
162
+
163
+ for (const schema of schemas) {
164
+ validateSchema(schema);
165
+ }
166
+ }
167
+
168
+ function validateSchema(schema) {
169
+ if (!schema || typeof schema._parse !== "function") {
170
+ throw new TypeError("Tuple elements must be valid schemas.");
171
+ }
172
+ }
173
+
174
+ function getType(value) {
175
+ if (value === null) return "null";
176
+ if (Array.isArray(value)) return "array";
177
+
178
+ return typeof value;
179
+ }
180
+
181
+ function isPromise(value) {
182
+ return (
183
+ value !== null &&
184
+ (typeof value === "object" || typeof value === "function") &&
185
+ typeof value.then === "function"
186
+ );
187
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Mint - Union Schema
3
+ *
4
+ * A value must validate against at least one schema.
5
+ */
6
+
7
+ import { Schema } from "../core/Schema.js";
8
+
9
+ export class UnionSchema extends Schema {
10
+ constructor(schemas) {
11
+ validateSchemas(schemas);
12
+
13
+ super({
14
+ type: "union",
15
+ options: Object.freeze([...schemas]),
16
+ });
17
+
18
+ this.options = Object.freeze([...schemas]);
19
+ }
20
+
21
+ _parse(context, value) {
22
+ const branchErrors = [];
23
+
24
+ for (const schema of this.options) {
25
+ const checkpoint = context.checkpoint();
26
+
27
+ const parsedValue = schema._parse(context, value);
28
+
29
+ if (isPromise(parsedValue)) {
30
+ throw new Error(
31
+ "Asynchronous union validation requires async parsing.",
32
+ );
33
+ }
34
+
35
+ const issues = context.getIssuesSince(checkpoint);
36
+
37
+ // A branch succeeds if it produces no issues.
38
+ if (issues.length === 0) {
39
+ return parsedValue;
40
+ }
41
+
42
+ // Preserve this branch's errors separately.
43
+ branchErrors.push([...issues]);
44
+
45
+ // Remove failed branch issues before trying another.
46
+ context.issues.splice(checkpoint);
47
+ }
48
+
49
+ context.addIssue({
50
+ code: "invalid_union",
51
+ message: "Input does not match any union option.",
52
+ unionErrors: branchErrors,
53
+ });
54
+
55
+ return value;
56
+ }
57
+
58
+ async _parseAsync(context, value) {
59
+ const branchErrors = [];
60
+
61
+ for (const schema of this.options) {
62
+ const checkpoint = context.checkpoint();
63
+
64
+ const parsedValue = await schema._parseAsync(context, value);
65
+
66
+ const issues = context.getIssuesSince(checkpoint);
67
+
68
+ // A branch succeeds if it produces no issues.
69
+ if (issues.length === 0) {
70
+ return parsedValue;
71
+ }
72
+
73
+ // Preserve this branch's errors separately.
74
+ branchErrors.push([...issues]);
75
+
76
+ // Remove failed branch issues before trying another.
77
+ context.issues.splice(checkpoint);
78
+ }
79
+
80
+ context.addIssue({
81
+ code: "invalid_union",
82
+ message: "Input does not match any union option.",
83
+ unionErrors: branchErrors,
84
+ });
85
+
86
+ return value;
87
+ }
88
+ }
89
+
90
+ function validateSchemas(schemas) {
91
+ if (!Array.isArray(schemas) || schemas.length === 0) {
92
+ throw new TypeError("UnionSchema expects a non-empty array of schemas.");
93
+ }
94
+
95
+ for (const schema of schemas) {
96
+ if (!schema || typeof schema._parse !== "function") {
97
+ throw new TypeError("Union options must be valid schemas.");
98
+ }
99
+ }
100
+ }
101
+
102
+ function isPromise(value) {
103
+ return (
104
+ value !== null &&
105
+ (typeof value === "object" || typeof value === "function") &&
106
+ typeof value.then === "function"
107
+ );
108
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Mint - Schema exports.
3
+ */
4
+
5
+ export { StringSchema } from "./StringSchema.js";
6
+ export { NumberSchema } from "./NumberSchema.js";
7
+ export { BooleanSchema } from "./BooleanSchema.js";
8
+ export { BigIntSchema } from "./BigIntSchema.js";
9
+ export { DateSchema } from "./DateSchema.js";
10
+ export { LiteralSchema } from "./LiteralSchema.js";
11
+ export { EnumSchema } from "./EnumSchema.js";
12
+ export { ArraySchema } from "./ArraySchema.js";
13
+ export { ObjectSchema } from "./ObjectSchema.js";
14
+ export { TupleSchema } from "./TupleSchema.js";
15
+ export { RecordSchema } from "./RecordSchema.js";
16
+ export { UnionSchema } from "./UnionSchema.js";
17
+ export { IntersectionSchema } from "./IntersectionSchema.js";
18
+ export { SetSchema } from "./SetSchema.js";
19
+ export { MapSchema } from "./MapSchema.js";
20
+ export { PromiseSchema } from "./PromiseSchema.js";
21
+ export { FunctionSchema } from "./FunctionSchema.js";
22
+ export { InstanceSchema } from "./InstanceSchema.js";
23
+
24
+ export {
25
+ NullSchema,
26
+ UndefinedSchema,
27
+ VoidSchema,
28
+ AnySchema,
29
+ UnknownSchema,
30
+ NeverSchema,
31
+ SymbolSchema,
32
+ } from "./SpecialSchemas.js";
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Mint - Object Utilities
3
+ */
4
+
5
+ export function isPlainObject(value) {
6
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
7
+ return false;
8
+ }
9
+
10
+ const prototype = Object.getPrototypeOf(value);
11
+
12
+ return prototype === Object.prototype || prototype === null;
13
+ }
14
+
15
+ export function getObjectKeys(value) {
16
+ return Object.keys(value);
17
+ }
18
+
19
+ export function hasOwn(object, key) {
20
+ return Object.prototype.hasOwnProperty.call(object, key);
21
+ }
22
+
23
+ export function omitKeys(object, keys) {
24
+ const excluded = new Set(keys);
25
+ const result = {};
26
+
27
+ for (const key of Object.keys(object)) {
28
+ if (!excluded.has(key)) {
29
+ result[key] = object[key];
30
+ }
31
+ }
32
+
33
+ return result;
34
+ }
35
+
36
+ export function pickKeys(object, keys) {
37
+ const selected = new Set(keys);
38
+ const result = {};
39
+
40
+ for (const key of Object.keys(object)) {
41
+ if (selected.has(key)) {
42
+ result[key] = object[key];
43
+ }
44
+ }
45
+
46
+ return result;
47
+ }