@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,69 @@
1
+ /**
2
+ * Mint - Enum Schema
3
+ *
4
+ * Validates that input belongs to a predefined set of values.
5
+ */
6
+
7
+ import { Schema } from "../core/Schema.js";
8
+
9
+ export class EnumSchema extends Schema {
10
+ constructor(values) {
11
+ const normalizedValues = normalizeValues(values);
12
+
13
+ if (normalizedValues.length === 0) {
14
+ throw new Error("EnumSchema requires at least one allowed value.");
15
+ }
16
+
17
+ super({
18
+ type: "enum",
19
+ values: Object.freeze([...normalizedValues]),
20
+ });
21
+
22
+ this.values = Object.freeze([...normalizedValues]);
23
+ }
24
+
25
+ _parse(context, value) {
26
+ if (!this.values.some((allowed) => Object.is(allowed, value))) {
27
+ context.addIssue({
28
+ code: "invalid_enum_value",
29
+ options: this.values,
30
+ received: value,
31
+ message: `Invalid enum value. Expected one of: ${this.values.map(formatValue).join(", ")}`,
32
+ });
33
+ }
34
+
35
+ return value;
36
+ }
37
+ }
38
+
39
+ function normalizeValues(values) {
40
+ if (Array.isArray(values)) {
41
+ return [...new Set(values)];
42
+ }
43
+
44
+ if (values !== null && typeof values === "object") {
45
+ return [...new Set(Object.values(values))];
46
+ }
47
+
48
+ throw new TypeError("EnumSchema expects an array or an object of values.");
49
+ }
50
+
51
+ function formatValue(value) {
52
+ if (typeof value === "string") {
53
+ return JSON.stringify(value);
54
+ }
55
+
56
+ if (typeof value === "bigint") {
57
+ return `${value}n`;
58
+ }
59
+
60
+ if (typeof value === "symbol") {
61
+ return value.toString();
62
+ }
63
+
64
+ if (value === undefined) {
65
+ return "undefined";
66
+ }
67
+
68
+ return String(value);
69
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Mint - Function Schema
3
+ *
4
+ * Validates callable functions.
5
+ * Optionally validates arguments and return values.
6
+ */
7
+
8
+ import { Schema } from "../core/Schema.js";
9
+ import { TupleSchema } from "./TupleSchema.js";
10
+
11
+ export class FunctionSchema extends Schema {
12
+ constructor(argsSchema = null, returnSchema = null) {
13
+ super({
14
+ type: "function",
15
+ argsSchema,
16
+ returnSchema,
17
+ });
18
+
19
+ this.argsSchema = argsSchema;
20
+ this.returnSchema = returnSchema;
21
+ }
22
+
23
+ _parse(context, value) {
24
+ if (typeof value !== "function") {
25
+ context.addIssue({
26
+ code: "invalid_type",
27
+ expected: "function",
28
+ received: typeof value,
29
+ message: `Expected function, received ${typeof value}`,
30
+ });
31
+
32
+ return value;
33
+ }
34
+
35
+ if (!this.argsSchema && !this.returnSchema) {
36
+ return value;
37
+ }
38
+
39
+ const originalFunction = value;
40
+ const argsSchema = this.argsSchema;
41
+ const returnSchema = this.returnSchema;
42
+
43
+ return function validatedFunction(...args) {
44
+ let validatedArgs = args;
45
+
46
+ if (argsSchema) {
47
+ validatedArgs = argsSchema.parse(args);
48
+ }
49
+
50
+ const result = originalFunction.apply(this, validatedArgs);
51
+
52
+ if (!returnSchema) {
53
+ return result;
54
+ }
55
+
56
+ // Support async function return validation.
57
+ if (isPromise(result)) {
58
+ return result.then((resolvedValue) =>
59
+ returnSchema.parseAsync(resolvedValue),
60
+ );
61
+ }
62
+
63
+ return returnSchema.parse(result);
64
+ };
65
+ }
66
+
67
+ /**
68
+ * Validate function arguments.
69
+ */
70
+ args(...schemas) {
71
+ return new FunctionSchema(new TupleSchema(schemas), this.returnSchema);
72
+ }
73
+
74
+ /**
75
+ * Validate function return values.
76
+ */
77
+ returns(schema) {
78
+ validateSchema(schema);
79
+
80
+ return new FunctionSchema(this.argsSchema, schema);
81
+ }
82
+ }
83
+
84
+ function validateSchema(schema) {
85
+ if (!schema || typeof schema._parse !== "function") {
86
+ throw new TypeError("Function return schema must be valid.");
87
+ }
88
+ }
89
+
90
+ function isPromise(value) {
91
+ return (
92
+ value !== null &&
93
+ (typeof value === "object" || typeof value === "function") &&
94
+ typeof value.then === "function"
95
+ );
96
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Mint - Instance Schema
3
+ *
4
+ * Validates instances of a specified constructor.
5
+ */
6
+
7
+ import { Schema } from "../core/Schema.js";
8
+
9
+ export class InstanceSchema extends Schema {
10
+ constructor(constructor) {
11
+ validateConstructor(constructor);
12
+
13
+ super({
14
+ type: "instance",
15
+ constructor,
16
+ });
17
+
18
+ this.constructorType = constructor;
19
+ }
20
+
21
+ _parse(context, value) {
22
+ if (!(value instanceof this.constructorType)) {
23
+ context.addIssue({
24
+ code: "invalid_type",
25
+ expected: this.constructorType.name || "instance",
26
+ received: getType(value),
27
+ message: `Expected instance of ${
28
+ this.constructorType.name || "specified class"
29
+ }`,
30
+ });
31
+
32
+ return value;
33
+ }
34
+
35
+ return value;
36
+ }
37
+ }
38
+
39
+ function validateConstructor(constructor) {
40
+ if (typeof constructor !== "function" || !constructor.prototype) {
41
+ throw new TypeError(
42
+ "InstanceSchema expects a class or constructor function.",
43
+ );
44
+ }
45
+ }
46
+
47
+ function getType(value) {
48
+ if (value === null) return "null";
49
+ if (Array.isArray(value)) return "array";
50
+
51
+ return typeof value;
52
+ }
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Mint - Intersection Schema
3
+ *
4
+ * A value must satisfy both schemas.
5
+ * Object outputs are combined.
6
+ */
7
+
8
+ import { Schema } from "../core/Schema.js";
9
+
10
+ export class IntersectionSchema extends Schema {
11
+ constructor(leftSchema, rightSchema) {
12
+ validateSchema(leftSchema);
13
+ validateSchema(rightSchema);
14
+
15
+ super({
16
+ type: "intersection",
17
+ left: leftSchema,
18
+ right: rightSchema,
19
+ });
20
+
21
+ this.leftSchema = leftSchema;
22
+ this.rightSchema = rightSchema;
23
+ }
24
+
25
+ _parse(context, value) {
26
+ const checkpoint = context.checkpoint();
27
+
28
+ const leftValue = this.leftSchema._parse(context, value);
29
+
30
+ if (isPromise(leftValue)) {
31
+ throw new Error(
32
+ "Asynchronous intersection validation requires async parsing.",
33
+ );
34
+ }
35
+
36
+ const rightValue = this.rightSchema._parse(context, value);
37
+
38
+ if (isPromise(rightValue)) {
39
+ throw new Error(
40
+ "Asynchronous intersection validation requires async parsing.",
41
+ );
42
+ }
43
+
44
+ if (context.hasIssuesSince(checkpoint)) {
45
+ return value;
46
+ }
47
+
48
+ const result = mergeValues(leftValue, rightValue);
49
+
50
+ if (!result.success) {
51
+ context.addIssue({
52
+ code: "invalid_intersection_types",
53
+ message: "Intersection results could not be combined.",
54
+ });
55
+
56
+ return value;
57
+ }
58
+
59
+ return result.data;
60
+ }
61
+
62
+ async _parseAsync(context, value) {
63
+ const checkpoint = context.checkpoint();
64
+
65
+ const leftValue = await this.leftSchema._parseAsync(
66
+ context,
67
+ value,
68
+ );
69
+
70
+ const rightValue = await this.rightSchema._parseAsync(
71
+ context,
72
+ value,
73
+ );
74
+
75
+ if (context.hasIssuesSince(checkpoint)) {
76
+ return value;
77
+ }
78
+
79
+ const result = mergeValues(leftValue, rightValue);
80
+
81
+ if (!result.success) {
82
+ context.addIssue({
83
+ code: "invalid_intersection_types",
84
+ message: "Intersection results could not be combined.",
85
+ });
86
+
87
+ return value;
88
+ }
89
+
90
+ return result.data;
91
+ }
92
+ }
93
+
94
+ function mergeValues(left, right) {
95
+ if (Object.is(left, right)) {
96
+ return {
97
+ success: true,
98
+ data: left,
99
+ };
100
+ }
101
+
102
+ if (isPlainObject(left) && isPlainObject(right)) {
103
+ const result = { ...left };
104
+
105
+ for (const key of Object.keys(right)) {
106
+ if (Object.prototype.hasOwnProperty.call(left, key)) {
107
+ const merged = mergeValues(left[key], right[key]);
108
+
109
+ if (!merged.success) {
110
+ return merged;
111
+ }
112
+
113
+ result[key] = merged.data;
114
+ } else {
115
+ result[key] = right[key];
116
+ }
117
+ }
118
+
119
+ return {
120
+ success: true,
121
+ data: result,
122
+ };
123
+ }
124
+
125
+ if (
126
+ Array.isArray(left) &&
127
+ Array.isArray(right) &&
128
+ left.length === right.length
129
+ ) {
130
+ const result = [];
131
+
132
+ for (let index = 0; index < left.length; index++) {
133
+ const merged = mergeValues(left[index], right[index]);
134
+
135
+ if (!merged.success) {
136
+ return merged;
137
+ }
138
+
139
+ result.push(merged.data);
140
+ }
141
+
142
+ return {
143
+ success: true,
144
+ data: result,
145
+ };
146
+ }
147
+
148
+ return {
149
+ success: false,
150
+ };
151
+ }
152
+
153
+ function isPlainObject(value) {
154
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
155
+ return false;
156
+ }
157
+
158
+ const prototype = Object.getPrototypeOf(value);
159
+
160
+ return prototype === Object.prototype || prototype === null;
161
+ }
162
+
163
+ function validateSchema(schema) {
164
+ if (!schema || typeof schema._parse !== "function") {
165
+ throw new TypeError("Intersection requires valid schemas.");
166
+ }
167
+ }
168
+
169
+ function isPromise(value) {
170
+ return (
171
+ value !== null &&
172
+ typeof value === "object" &&
173
+ typeof value.then === "function"
174
+ );
175
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Mint - Literal Schema
3
+ *
4
+ * Validates that input exactly matches a specified value.
5
+ */
6
+
7
+ import { Schema } from "../core/Schema.js";
8
+
9
+ export class LiteralSchema extends Schema {
10
+ constructor(expectedValue) {
11
+ super({
12
+ type: "literal",
13
+ value: expectedValue,
14
+ });
15
+
16
+ this.expectedValue = expectedValue;
17
+ }
18
+
19
+ _parse(context, value) {
20
+ if (!Object.is(value, this.expectedValue)) {
21
+ context.addIssue({
22
+ code: "invalid_literal",
23
+ expected: this.expectedValue,
24
+ received: value,
25
+ message: `Expected ${formatValue(this.expectedValue)}`,
26
+ });
27
+ }
28
+
29
+ return value;
30
+ }
31
+ }
32
+
33
+ function formatValue(value) {
34
+ if (typeof value === "string") {
35
+ return JSON.stringify(value);
36
+ }
37
+
38
+ if (typeof value === "bigint") {
39
+ return `${value}n`;
40
+ }
41
+
42
+ if (typeof value === "symbol") {
43
+ return value.toString();
44
+ }
45
+
46
+ if (value === undefined) {
47
+ return "undefined";
48
+ }
49
+
50
+ return String(value);
51
+ }
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Mint - Map Schema
3
+ *
4
+ * Validates native JavaScript Maps.
5
+ * Supports key and value validation.
6
+ */
7
+
8
+ import { Schema } from "../core/Schema.js";
9
+
10
+ export class MapSchema extends Schema {
11
+ constructor(keySchema, valueSchema, checks = []) {
12
+ validateSchema(keySchema);
13
+ validateSchema(valueSchema);
14
+
15
+ super({
16
+ type: "map",
17
+ keySchema,
18
+ valueSchema,
19
+ checks: Object.freeze([...checks]),
20
+ });
21
+
22
+ this.keySchema = keySchema;
23
+ this.valueSchema = valueSchema;
24
+ this.checks = Object.freeze([...checks]);
25
+ }
26
+
27
+ _parse(context, value) {
28
+ if (!(value instanceof Map)) {
29
+ context.addIssue({
30
+ code: "invalid_type",
31
+ expected: "map",
32
+ received: getType(value),
33
+ message: `Expected Map, received ${getType(value)}`,
34
+ });
35
+
36
+ return value;
37
+ }
38
+
39
+ const size = value.size;
40
+
41
+ for (const check of this.checks) {
42
+ const valid = check.validate(size);
43
+
44
+ if (!valid) {
45
+ context.addIssue({
46
+ code: check.code,
47
+ minimum: check.minimum,
48
+ maximum: check.maximum,
49
+ inclusive: check.inclusive,
50
+ type: "map",
51
+ message: check.message,
52
+ });
53
+ }
54
+ }
55
+
56
+ const output = new Map();
57
+
58
+ for (const [key, entryValue] of value) {
59
+ const keyContext = context.child(key, key);
60
+
61
+ const parsedKey = this.keySchema._parse(keyContext, key);
62
+
63
+ if (isPromise(parsedKey)) {
64
+ throw new Error(
65
+ "Asynchronous Map key validation requires async parsing.",
66
+ );
67
+ }
68
+
69
+ const valueContext = context.child(key, entryValue);
70
+
71
+ const parsedValue = this.valueSchema._parse(valueContext, entryValue);
72
+
73
+ if (isPromise(parsedValue)) {
74
+ throw new Error(
75
+ "Asynchronous Map value validation requires async parsing.",
76
+ );
77
+ }
78
+
79
+ output.set(parsedKey, parsedValue);
80
+ }
81
+
82
+ return output;
83
+ }
84
+
85
+ async _parseAsync(context, value) {
86
+ if (!(value instanceof Map)) {
87
+ context.addIssue({
88
+ code: "invalid_type",
89
+ expected: "map",
90
+ received: getType(value),
91
+ message: `Expected Map, received ${getType(value)}`,
92
+ });
93
+
94
+ return value;
95
+ }
96
+
97
+ const size = value.size;
98
+
99
+ for (const check of this.checks) {
100
+ const valid = check.validate(size);
101
+
102
+ if (!valid) {
103
+ context.addIssue({
104
+ code: check.code,
105
+ minimum: check.minimum,
106
+ maximum: check.maximum,
107
+ inclusive: check.inclusive,
108
+ type: "map",
109
+ message: check.message,
110
+ });
111
+ }
112
+ }
113
+
114
+ const output = new Map();
115
+
116
+ for (const [key, entryValue] of value) {
117
+ const keyContext = context.child(key, key);
118
+
119
+ const parsedKey = await this.keySchema._parseAsync(keyContext, key);
120
+
121
+ const valueContext = context.child(key, entryValue);
122
+
123
+ const parsedValue = await this.valueSchema._parseAsync(
124
+ valueContext,
125
+ entryValue,
126
+ );
127
+
128
+ output.set(parsedKey, parsedValue);
129
+ }
130
+
131
+ return output;
132
+ }
133
+
134
+ min(minimum, message) {
135
+ return this._addSizeCheck({
136
+ code: "too_small",
137
+ minimum,
138
+ inclusive: true,
139
+ message: message ?? `Map must contain at least ${minimum} entry(s)`,
140
+ validate: (size) => size >= minimum,
141
+ });
142
+ }
143
+
144
+ max(maximum, message) {
145
+ return this._addSizeCheck({
146
+ code: "too_big",
147
+ maximum,
148
+ inclusive: true,
149
+ message: message ?? `Map must contain at most ${maximum} entry(s)`,
150
+ validate: (size) => size <= maximum,
151
+ });
152
+ }
153
+
154
+ size(length, message) {
155
+ return this._addSizeCheck({
156
+ code: "invalid_size",
157
+ minimum: length,
158
+ maximum: length,
159
+ inclusive: true,
160
+ message: message ?? `Map must contain exactly ${length} entry(s)`,
161
+ validate: (size) => size === length,
162
+ });
163
+ }
164
+
165
+ nonempty(message) {
166
+ return this.min(1, message);
167
+ }
168
+
169
+ _addSizeCheck(check) {
170
+ return new MapSchema(this.keySchema, this.valueSchema, [
171
+ ...this.checks,
172
+ check,
173
+ ]);
174
+ }
175
+ }
176
+
177
+ function validateSchema(schema) {
178
+ if (!schema || typeof schema._parse !== "function") {
179
+ throw new TypeError("Map key and value must be valid schemas.");
180
+ }
181
+ }
182
+
183
+ function getType(value) {
184
+ if (value === null) return "null";
185
+
186
+ if (value instanceof Map) {
187
+ return "map";
188
+ }
189
+
190
+ if (Array.isArray(value)) {
191
+ return "array";
192
+ }
193
+
194
+ return typeof value;
195
+ }
196
+
197
+ function isPromise(value) {
198
+ return (
199
+ value !== null &&
200
+ typeof value === "object" &&
201
+ typeof value.then === "function"
202
+ );
203
+ }