Skip to content

Commit a836472

Browse files
authored
Merge pull request #24 from firefliesai/feature-sc-93154-add-date-type-support-with-string-format-date
2 parents e55bf30 + 986079d commit a836472

File tree

10 files changed

+863
-8
lines changed

10 files changed

+863
-8
lines changed

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Schema Forge is a powerful TypeScript library that transforms your TypeScript cl
2424
- [Structured Output Formatting](#structured-output-formatting)
2525
- [Property Overrides](#property-overrides)
2626
- [Nested Objects and Arrays](#nested-objects-and-arrays)
27+
- [Class-Validator Integration](#class-validator-integration)
2728
- [Using TypeScript Enums](#using-typescript-enums)
2829
- [Dynamic Schema Updates](#dynamic-schema-updates)
2930
- [Using Direct JSON Schema Converters](#using-direct-json-schema-converters)
@@ -67,6 +68,7 @@ Schema Forge is a powerful TypeScript library that transforms your TypeScript cl
6768
- 📝 Built-in structured output formatting for various LLM providers
6869
- 📦 TypeScript-first with full type safety and inference
6970
- 🪶 Lightweight with minimal dependencies (only requires reflect-metadata)
71+
- 🔗 Automatic class-validator integration: infers JSON Schema constraints from validation decorators when used
7072

7173
## Who Should Use This Library?
7274

@@ -496,6 +498,87 @@ Schema Forge uses TypeScript's reflection capabilities to automatically infer mo
496498

497499
All other primitive types (string, number, boolean) and custom classes are automatically inferred without additional type specification.
498500

501+
### Class-Validator Integration
502+
503+
If you're using [class-validator](https://github.com/typestack/class-validator) decorators in your project, Schema Forge will automatically infer JSON Schema constraints from them. This means you can define validation rules once and have them reflected in your generated schemas.
504+
505+
Supported class-validator decorators:
506+
507+
| Decorator | JSON Schema Property |
508+
|-----------|---------------------|
509+
| `@ArrayMaxSize(n)` | `maxItems: n` |
510+
| `@ArrayMinSize(n)` | `minItems: n` |
511+
| `@ArrayUnique()` | `uniqueItems: true` |
512+
| `@ArrayNotEmpty()` | `minItems: 1` |
513+
| `@Max(n)` | `maximum: n` |
514+
| `@Min(n)` | `minimum: n` |
515+
| `@IsInt()` | `type: 'integer'` |
516+
| `@MinLength(n)` | `minLength: n` |
517+
| `@MaxLength(n)` | `maxLength: n` |
518+
| `@IsUrl()` | `format: 'uri'` |
519+
| `@IsEmail()` | `format: 'email'` |
520+
| `@IsPositive()` | `minimum: 1` |
521+
522+
Example:
523+
524+
```typescript
525+
import { ToolProp } from '@firefliesai/schema-forge';
526+
import { Min, Max, IsInt, MinLength, MaxLength, IsUrl, IsPositive } from 'class-validator';
527+
528+
class ProductInput {
529+
@ToolProp({ description: 'Product name' })
530+
@MinLength(2)
531+
@MaxLength(100)
532+
name: string;
533+
534+
@ToolProp({ description: 'Product price in cents' })
535+
@IsInt()
536+
@IsPositive()
537+
price: number;
538+
539+
@ToolProp({ description: 'Stock quantity' })
540+
@Min(0)
541+
@Max(10000)
542+
quantity: number;
543+
544+
@ToolProp({ description: 'Product page URL' })
545+
@IsUrl()
546+
url: string;
547+
}
548+
549+
// The generated schema will include the constraints:
550+
// {
551+
// properties: {
552+
// name: { type: 'string', minLength: 2, maxLength: 100, ... },
553+
// price: { type: 'integer', minimum: 1, ... },
554+
// quantity: { type: 'number', minimum: 0, maximum: 10000, ... },
555+
// url: { type: 'string', format: 'uri', ... }
556+
// }
557+
// }
558+
```
559+
560+
You can also specify these constraints directly in `@ToolProp` options without using class-validator:
561+
562+
```typescript
563+
class ProductInput {
564+
@ToolProp({
565+
description: 'Product name',
566+
minLength: 2,
567+
maxLength: 100
568+
})
569+
name: string;
570+
571+
@ToolProp({
572+
description: 'Product price in cents',
573+
type: 'integer',
574+
minimum: 1
575+
})
576+
price: number;
577+
}
578+
```
579+
580+
When both class-validator decorators and explicit `@ToolProp` options are present, the explicit options take precedence.
581+
499582
### Using TypeScript Enums
500583

501584
Schema Forge works well with native TypeScript enums:

package-lock.json

Lines changed: 39 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@firefliesai/schema-forge",
3-
"version": "1.0.4",
3+
"version": "2.0.0",
44
"main": "dist/lib/index.js",
55
"types": "dist/lib/index.d.ts",
66
"exports": {
@@ -78,6 +78,7 @@
7878
"@google/generative-ai": "^0.24.0",
7979
"@types/jest": "^29.5.14",
8080
"@types/reflect-metadata": "^0.0.5",
81+
"class-validator": "^0.14.3",
8182
"env-cmd": "^10.1.0",
8283
"eslint": "^9.22.0",
8384
"eslint-config-prettier": "^10.1.1",

0 commit comments

Comments
 (0)