@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,185 @@
1
+ /**
2
+ * Mint - Set Schema
3
+ *
4
+ * Validates native JavaScript Sets.
5
+ * Supports element validation and size constraints.
6
+ */
7
+
8
+ import { Schema } from "../core/Schema.js";
9
+
10
+ export class SetSchema extends Schema {
11
+ constructor(valueSchema, checks = []) {
12
+ validateSchema(valueSchema);
13
+
14
+ super({
15
+ type: "set",
16
+ valueSchema,
17
+ checks: Object.freeze([...checks]),
18
+ });
19
+
20
+ this.valueSchema = valueSchema;
21
+ this.checks = Object.freeze([...checks]);
22
+ }
23
+
24
+ _parse(context, value) {
25
+ if (!(value instanceof Set)) {
26
+ context.addIssue({
27
+ code: "invalid_type",
28
+ expected: "set",
29
+ received: getType(value),
30
+ message: `Expected Set, received ${getType(value)}`,
31
+ });
32
+
33
+ return value;
34
+ }
35
+
36
+ const size = value.size;
37
+
38
+ for (const check of this.checks) {
39
+ const valid = check.validate(size);
40
+
41
+ if (!valid) {
42
+ context.addIssue({
43
+ code: check.code,
44
+ minimum: check.minimum,
45
+ maximum: check.maximum,
46
+ inclusive: check.inclusive,
47
+ type: "set",
48
+ message: check.message,
49
+ });
50
+ }
51
+ }
52
+
53
+ const output = new Set();
54
+ let index = 0;
55
+
56
+ for (const item of value) {
57
+ const childContext = context.child(index, item);
58
+
59
+ const parsedValue = this.valueSchema._parse(childContext, item);
60
+
61
+ if (isPromise(parsedValue)) {
62
+ throw new Error("Asynchronous Set validation requires async parsing.");
63
+ }
64
+
65
+ output.add(parsedValue);
66
+ index++;
67
+ }
68
+
69
+ return output;
70
+ }
71
+
72
+ async _parseAsync(context, value) {
73
+ if (!(value instanceof Set)) {
74
+ context.addIssue({
75
+ code: "invalid_type",
76
+ expected: "set",
77
+ received: getType(value),
78
+ message: `Expected Set, received ${getType(value)}`,
79
+ });
80
+
81
+ return value;
82
+ }
83
+
84
+ const size = value.size;
85
+
86
+ for (const check of this.checks) {
87
+ const valid = check.validate(size);
88
+
89
+ if (!valid) {
90
+ context.addIssue({
91
+ code: check.code,
92
+ minimum: check.minimum,
93
+ maximum: check.maximum,
94
+ inclusive: check.inclusive,
95
+ type: "set",
96
+ message: check.message,
97
+ });
98
+ }
99
+ }
100
+
101
+ const output = new Set();
102
+ let index = 0;
103
+
104
+ for (const item of value) {
105
+ const childContext = context.child(index, item);
106
+
107
+ const parsedValue = await this.valueSchema._parseAsync(
108
+ childContext,
109
+ item,
110
+ );
111
+
112
+ output.add(parsedValue);
113
+ index++;
114
+ }
115
+
116
+ return output;
117
+ }
118
+
119
+ min(minimum, message) {
120
+ return this._addSizeCheck({
121
+ code: "too_small",
122
+ minimum,
123
+ inclusive: true,
124
+ message: message ?? `Set must contain at least ${minimum} element(s)`,
125
+ validate: (size) => size >= minimum,
126
+ });
127
+ }
128
+
129
+ max(maximum, message) {
130
+ return this._addSizeCheck({
131
+ code: "too_big",
132
+ maximum,
133
+ inclusive: true,
134
+ message: message ?? `Set must contain at most ${maximum} element(s)`,
135
+ validate: (size) => size <= maximum,
136
+ });
137
+ }
138
+
139
+ size(length, message) {
140
+ return this._addSizeCheck({
141
+ code: "invalid_size",
142
+ minimum: length,
143
+ maximum: length,
144
+ inclusive: true,
145
+ message: message ?? `Set must contain exactly ${length} element(s)`,
146
+ validate: (size) => size === length,
147
+ });
148
+ }
149
+
150
+ nonempty(message) {
151
+ return this.min(1, message);
152
+ }
153
+
154
+ _addSizeCheck(check) {
155
+ return new SetSchema(this.valueSchema, [...this.checks, check]);
156
+ }
157
+ }
158
+
159
+ function validateSchema(schema) {
160
+ if (!schema || typeof schema._parse !== "function") {
161
+ throw new TypeError("SetSchema requires a valid element schema.");
162
+ }
163
+ }
164
+
165
+ function getType(value) {
166
+ if (value === null) return "null";
167
+
168
+ if (value instanceof Set) {
169
+ return "set";
170
+ }
171
+
172
+ if (Array.isArray(value)) {
173
+ return "array";
174
+ }
175
+
176
+ return typeof value;
177
+ }
178
+
179
+ function isPromise(value) {
180
+ return (
181
+ value !== null &&
182
+ typeof value === "object" &&
183
+ typeof value.then === "function"
184
+ );
185
+ }
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Mint - Special Schemas
3
+ *
4
+ * Schemas for null, undefined, void, any, unknown,
5
+ * never, and symbol.
6
+ */
7
+
8
+ import { Schema } from "../core/Schema.js";
9
+
10
+ /**
11
+ * Accepts only null.
12
+ */
13
+ export class NullSchema extends Schema {
14
+ constructor() {
15
+ super({ type: "null" });
16
+ }
17
+
18
+ _parse(context, value) {
19
+ if (value !== null) {
20
+ context.addIssue({
21
+ code: "invalid_type",
22
+ expected: "null",
23
+ received: getType(value),
24
+ message: `Expected null, received ${getType(value)}`,
25
+ });
26
+ }
27
+
28
+ return value;
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Accepts only undefined.
34
+ */
35
+ export class UndefinedSchema extends Schema {
36
+ constructor() {
37
+ super({ type: "undefined" });
38
+ }
39
+
40
+ _parse(context, value) {
41
+ if (value !== undefined) {
42
+ context.addIssue({
43
+ code: "invalid_type",
44
+ expected: "undefined",
45
+ received: getType(value),
46
+ message: `Expected undefined, received ${getType(value)}`,
47
+ });
48
+ }
49
+
50
+ return value;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Accepts undefined.
56
+ * Similar to undefined schema but semantically represents void.
57
+ */
58
+ export class VoidSchema extends Schema {
59
+ constructor() {
60
+ super({ type: "void" });
61
+ }
62
+
63
+ _parse(context, value) {
64
+ if (value !== undefined) {
65
+ context.addIssue({
66
+ code: "invalid_type",
67
+ expected: "void",
68
+ received: getType(value),
69
+ message: `Expected void, received ${getType(value)}`,
70
+ });
71
+ }
72
+
73
+ return value;
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Accepts every value without validation.
79
+ */
80
+ export class AnySchema extends Schema {
81
+ constructor() {
82
+ super({ type: "any" });
83
+ }
84
+
85
+ _parse(context, value) {
86
+ return value;
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Accepts every value without validation.
92
+ *
93
+ * Unlike AnySchema, this is intended to represent
94
+ * an unknown value requiring further narrowing.
95
+ */
96
+ export class UnknownSchema extends Schema {
97
+ constructor() {
98
+ super({ type: "unknown" });
99
+ }
100
+
101
+ _parse(context, value) {
102
+ return value;
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Rejects every value.
108
+ */
109
+ export class NeverSchema extends Schema {
110
+ constructor() {
111
+ super({ type: "never" });
112
+ }
113
+
114
+ _parse(context, value) {
115
+ context.addIssue({
116
+ code: "invalid_type",
117
+ expected: "never",
118
+ received: getType(value),
119
+ message: "Expected never",
120
+ });
121
+
122
+ return value;
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Accepts only symbols.
128
+ */
129
+ export class SymbolSchema extends Schema {
130
+ constructor() {
131
+ super({ type: "symbol" });
132
+ }
133
+
134
+ _parse(context, value) {
135
+ if (typeof value !== "symbol") {
136
+ context.addIssue({
137
+ code: "invalid_type",
138
+ expected: "symbol",
139
+ received: getType(value),
140
+ message: `Expected symbol, received ${getType(value)}`,
141
+ });
142
+ }
143
+
144
+ return value;
145
+ }
146
+ }
147
+
148
+ function getType(value) {
149
+ if (value === null) return "null";
150
+ if (Array.isArray(value)) return "array";
151
+ if (value instanceof Date) {
152
+ return Number.isNaN(value.getTime()) ? "invalid date" : "date";
153
+ }
154
+
155
+ return typeof value;
156
+ }
@@ -0,0 +1,222 @@
1
+ /**
2
+ * Mint - String Schema
3
+ */
4
+
5
+ import { Schema } from "../core/Schema.js";
6
+
7
+ const EMAIL_REGEX =
8
+ /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
9
+
10
+ export class StringSchema extends Schema {
11
+ constructor(checks = []) {
12
+ super({ type: "string" });
13
+ this.checks = [...checks];
14
+ }
15
+
16
+ _addCheck(check) {
17
+ return new StringSchema([
18
+ ...this.checks,
19
+ check,
20
+ ]);
21
+ }
22
+
23
+ _parse(context, value) {
24
+ if (typeof value !== "string") {
25
+ context.addIssue({
26
+ code: "invalid_type",
27
+ expected: "string",
28
+ received: getType(value),
29
+ message: `Expected string, received ${getType(value)}`,
30
+ });
31
+
32
+ return value;
33
+ }
34
+
35
+ let parsed = value;
36
+
37
+ for (const check of this.checks) {
38
+ if (check.kind === "transform") {
39
+ parsed = check.transform(parsed);
40
+ continue;
41
+ }
42
+
43
+ const valid = check.validate(parsed);
44
+
45
+ if (!valid) {
46
+ context.addIssue({
47
+ code: check.code,
48
+ message: check.message,
49
+ ...check.metadata,
50
+ });
51
+ }
52
+ }
53
+
54
+ return parsed;
55
+ }
56
+
57
+ min(value, message) {
58
+ return this._addCheck({
59
+ kind: "check",
60
+ code: "too_small",
61
+ validate: (input) => input.length >= value,
62
+ message: message ?? `String must contain at least ${value} character(s)`,
63
+ metadata: {
64
+ minimum: value,
65
+ inclusive: true,
66
+ type: "string",
67
+ },
68
+ });
69
+ }
70
+
71
+ max(value, message) {
72
+ return this._addCheck({
73
+ kind: "check",
74
+ code: "too_big",
75
+ validate: (input) => input.length <= value,
76
+ message: message ?? `String must contain at most ${value} character(s)`,
77
+ metadata: {
78
+ maximum: value,
79
+ inclusive: true,
80
+ type: "string",
81
+ },
82
+ });
83
+ }
84
+
85
+ length(value, message) {
86
+ return this._addCheck({
87
+ kind: "check",
88
+ code: "invalid_length",
89
+ validate: (input) => input.length === value,
90
+ message: message ?? `String must contain exactly ${value} character(s)`,
91
+ metadata: {
92
+ length: value,
93
+ type: "string",
94
+ },
95
+ });
96
+ }
97
+
98
+ regex(expression, message) {
99
+ return this._addCheck({
100
+ kind: "check",
101
+ code: "invalid_string",
102
+ validate: (input) => {
103
+ expression.lastIndex = 0;
104
+ return expression.test(input);
105
+ },
106
+ message: message ?? "Invalid string format",
107
+ metadata: {
108
+ validation: "regex",
109
+ },
110
+ });
111
+ }
112
+
113
+ email(message) {
114
+ return this._addCheck({
115
+ kind: "check",
116
+ code: "invalid_string",
117
+ validate: (input) => EMAIL_REGEX.test(input),
118
+ message: message ?? "Invalid email address",
119
+ metadata: {
120
+ validation: "email",
121
+ },
122
+ });
123
+ }
124
+
125
+ url(message) {
126
+ return this._addCheck({
127
+ kind: "check",
128
+ code: "invalid_string",
129
+ validate: (input) => {
130
+ try {
131
+ const url = new URL(input);
132
+ return Boolean(url.protocol && url.hostname);
133
+ } catch {
134
+ return false;
135
+ }
136
+ },
137
+ message: message ?? "Invalid URL",
138
+ metadata: {
139
+ validation: "url",
140
+ },
141
+ });
142
+ }
143
+
144
+ uuid(message) {
145
+ return this._addCheck({
146
+ kind: "check",
147
+ code: "invalid_string",
148
+ validate: (input) =>
149
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(input),
150
+ message: message ?? "Invalid UUID",
151
+ metadata: {
152
+ validation: "uuid",
153
+ },
154
+ });
155
+ }
156
+
157
+ startsWith(value, message) {
158
+ return this._addCheck({
159
+ kind: "check",
160
+ code: "invalid_string",
161
+ validate: (input) => input.startsWith(value),
162
+ message: message ?? `String must start with "${value}"`,
163
+ metadata: {
164
+ validation: "startsWith",
165
+ value,
166
+ },
167
+ });
168
+ }
169
+
170
+ endsWith(value, message) {
171
+ return this._addCheck({
172
+ kind: "check",
173
+ code: "invalid_string",
174
+ validate: (input) => input.endsWith(value),
175
+ message: message ?? `String must end with "${value}"`,
176
+ metadata: {
177
+ validation: "endsWith",
178
+ value,
179
+ },
180
+ });
181
+ }
182
+
183
+ includes(value, message) {
184
+ return this._addCheck({
185
+ kind: "check",
186
+ code: "invalid_string",
187
+ validate: (input) => input.includes(value),
188
+ message: message ?? `String must contain "${value}"`,
189
+ metadata: {
190
+ validation: "includes",
191
+ value,
192
+ },
193
+ });
194
+ }
195
+
196
+ trim() {
197
+ return this._addCheck({
198
+ kind: "transform",
199
+ transform: (input) => input.trim(),
200
+ });
201
+ }
202
+
203
+ lowercase() {
204
+ return this._addCheck({
205
+ kind: "transform",
206
+ transform: (input) => input.toLowerCase(),
207
+ });
208
+ }
209
+
210
+ uppercase() {
211
+ return this._addCheck({
212
+ kind: "transform",
213
+ transform: (input) => input.toUpperCase(),
214
+ });
215
+ }
216
+ }
217
+
218
+ function getType(value) {
219
+ if (value === null) return "null";
220
+ if (Array.isArray(value)) return "array";
221
+ return typeof value;
222
+ }