@forgehive/schema 0.1.3 → 0.1.4

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 ADDED
@@ -0,0 +1,161 @@
1
+ # @forgehive/schema
2
+
3
+ A powerful schema validation library built on top of Zod, providing type-safe validation and type inference.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @forgehive/schema
9
+ ```
10
+
11
+ ## Overview
12
+
13
+ The `@forgehive/schema` package provides a wrapper around Zod with additional features:
14
+
15
+ - Type-safe schema definitions
16
+ - Built-in validation methods
17
+ - Schema description capabilities
18
+ - Type inference utilities
19
+
20
+ ## Basic Usage
21
+
22
+ Here's a simple example of creating and using a schema:
23
+
24
+ ```typescript
25
+ import { Schema } from '@forgehive/schema';
26
+
27
+ // Create a schema with validation
28
+ const userSchema = new Schema({
29
+ name: Schema.string(),
30
+ age: Schema.number().min(0).max(120),
31
+ email: Schema.string().email(),
32
+ tags: Schema.array(Schema.string())
33
+ });
34
+
35
+ // Validate data
36
+ const result = userSchema.safeParse({
37
+ name: 'John Doe',
38
+ age: 30,
39
+ email: 'john@example.com',
40
+ tags: ['user', 'active']
41
+ });
42
+
43
+ if (result.success) {
44
+ // TypeScript knows the shape of the data
45
+ const user = result.data;
46
+ console.log(user.name); // TypeScript knows this is a string
47
+ }
48
+ ```
49
+
50
+ ## Schema Types
51
+
52
+ The package provides several schema types:
53
+
54
+ ```typescript
55
+ // Basic types
56
+ Schema.string()
57
+ Schema.number()
58
+ Schema.boolean()
59
+ Schema.date()
60
+
61
+ // Arrays
62
+ Schema.array(Schema.string())
63
+ Schema.array(Schema.number())
64
+ Schema.array(Schema.boolean())
65
+ Schema.array(Schema.date())
66
+
67
+ // Records
68
+ Schema.stringRecord() // Record<string, string>
69
+ Schema.numberRecord() // Record<string, number>
70
+ Schema.booleanRecord() // Record<string, boolean>
71
+ Schema.mixedRecord() // Record<string, string | number | boolean>
72
+ ```
73
+
74
+ ## Validation
75
+
76
+ Schemas provide several validation methods:
77
+
78
+ ```typescript
79
+ const schema = new Schema({
80
+ name: Schema.string(),
81
+ age: Schema.number()
82
+ });
83
+
84
+ // Parse and throw on error
85
+ const data = schema.parse({ name: 'John', age: 30 });
86
+
87
+ // Safe parse with result object
88
+ const result = schema.safeParse({ name: 'John', age: 30 });
89
+ if (result.success) {
90
+ const data = result.data;
91
+ } else {
92
+ const errors = result.error;
93
+ }
94
+
95
+ // Validate without parsing
96
+ const isValid = schema.validate({ name: 'John', age: 30 });
97
+ ```
98
+
99
+ ## Schema Description
100
+
101
+ You can get a description of the schema structure:
102
+
103
+ ```typescript
104
+ const schema = new Schema({
105
+ name: Schema.string(),
106
+ age: Schema.number().min(0),
107
+ email: Schema.string().email()
108
+ });
109
+
110
+ const description = schema.describe();
111
+ // Returns:
112
+ // {
113
+ // name: { type: 'string' },
114
+ // age: { type: 'number', validations: { min: 0 } },
115
+ // email: { type: 'string', validations: { email: true } }
116
+ // }
117
+ ```
118
+
119
+ ## Type Inference
120
+
121
+ The package provides type utilities for inferring types from schemas:
122
+
123
+ ```typescript
124
+ import { Schema, type InferSchema } from '@forgehive/schema';
125
+
126
+ const schema = new Schema({
127
+ name: Schema.string(),
128
+ age: Schema.number()
129
+ });
130
+
131
+ // Infer the type from the schema
132
+ type User = InferSchema<typeof schema>;
133
+ // Type is: { name: string; age: number }
134
+ ```
135
+
136
+ ## API Reference
137
+
138
+ ### `Schema` Class
139
+
140
+ - `constructor(fields: Record<string, SchemaType>)`: Creates a new schema
141
+ - `parse(data: unknown)`: Parses and validates data, throws on error
142
+ - `safeParse(data: unknown)`: Safely parses and validates data
143
+ - `validate(data: unknown)`: Validates data without parsing
144
+ - `describe()`: Returns a description of the schema structure
145
+ - `asZod()`: Returns the underlying Zod schema
146
+
147
+ ### Static Methods
148
+
149
+ - `string()`: Creates a string schema
150
+ - `number()`: Creates a number schema
151
+ - `boolean()`: Creates a boolean schema
152
+ - `date()`: Creates a date schema
153
+ - `array(type)`: Creates an array schema
154
+ - `stringRecord()`: Creates a record schema with string values
155
+ - `numberRecord()`: Creates a record schema with number values
156
+ - `booleanRecord()`: Creates a record schema with boolean values
157
+ - `mixedRecord()`: Creates a record schema with mixed values
158
+
159
+ ## License
160
+
161
+ MIT
package/dist/index.d.ts CHANGED
@@ -129,5 +129,10 @@ export declare class Schema<T extends Record<string, z.ZodType<string | boolean
129
129
  * @returns An object describing the schema structure with type information
130
130
  */
131
131
  describe(): SchemaDescription;
132
+ /**
133
+ * Returns the underlying Zod schema object
134
+ * @returns The Zod schema object
135
+ */
136
+ asZod(): z.ZodObject<T>;
132
137
  }
133
138
  export default Schema;
package/dist/index.js CHANGED
@@ -280,6 +280,13 @@ class Schema {
280
280
  }
281
281
  return description;
282
282
  }
283
+ /**
284
+ * Returns the underlying Zod schema object
285
+ * @returns The Zod schema object
286
+ */
287
+ asZod() {
288
+ return this.schema;
289
+ }
283
290
  }
284
291
  exports.Schema = Schema;
285
292
  exports.default = Schema;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forgehive/schema",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -355,6 +355,14 @@ export class Schema<T extends Record<string, z.ZodType<string | boolean | number
355
355
 
356
356
  return description
357
357
  }
358
+
359
+ /**
360
+ * Returns the underlying Zod schema object
361
+ * @returns The Zod schema object
362
+ */
363
+ asZod(): z.ZodObject<T> {
364
+ return this.schema
365
+ }
358
366
  }
359
367
 
360
368
  export default Schema