@janus-validator/dsl
Advanced tools
@@ -5,5 +5,4 @@ /** | ||
| * This package re-exports the DSL from @janus-validator/core for convenience. | ||
| * You can use either: | ||
| * - import { B, S, I } from '@janus-validator/dsl' | ||
| * - import { B, S, I } from '@janus-validator/core/DSL' | ||
| * Use: | ||
| * - import { B, U, S, I } from '@janus-validator/dsl' | ||
| * | ||
@@ -10,0 +9,0 @@ * @example |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/com/techlloyd/janus/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,cAAc,2BAA2B,CAAC"} | ||
| {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/com/techlloyd/janus/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,cAAc,2BAA2B,CAAC"} |
@@ -6,5 +6,4 @@ "use strict"; | ||
| * This package re-exports the DSL from @janus-validator/core for convenience. | ||
| * You can use either: | ||
| * - import { B, S, I } from '@janus-validator/dsl' | ||
| * - import { B, S, I } from '@janus-validator/core/DSL' | ||
| * Use: | ||
| * - import { B, U, S, I } from '@janus-validator/dsl' | ||
| * | ||
@@ -11,0 +10,0 @@ * @example |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/com/techlloyd/janus/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;GAkBG;;;;;;;;;;;;;;;;AAEH,4DAA0C"} | ||
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/com/techlloyd/janus/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;GAiBG;;;;;;;;;;;;;;;;AAEH,4DAA0C"} |
+1
-1
| { | ||
| "name": "@janus-validator/dsl", | ||
| "version": "0.5.0", | ||
| "version": "0.5.1", | ||
| "description": "Concise DSL for Janus Validator - short aliases for all combinators", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
+73
-1
@@ -5,2 +5,7 @@ # @janus-validator/dsl | ||
| The DSL is “just syntax”: it builds validators from `@janus-validator/core`, so you still get: | ||
| - ✅ **Forward validation**: `validate(unknown)` | ||
| - 🎲 **Backwards generation**: `new Generator(rng).generate(validator)` | ||
| ## Installation | ||
@@ -15,3 +20,3 @@ | ||
| ```typescript | ||
| import { B, S, I, N, L, R, O, Bytes, Or, Seq, optional, oneOrMore } from '@janus-validator/dsl'; | ||
| import { B, U, S, I, N, L, R, O, Bytes, Or, Seq, optional, oneOrMore } from '@janus-validator/dsl'; | ||
| import { Generator } from '@janus-validator/core'; | ||
@@ -44,2 +49,20 @@ | ||
| ## Why this is powerful | ||
| The same validator definition can be used: | ||
| - In production (validate API requests / config / events) | ||
| - In tests (generate fixtures that must satisfy the same constraints) | ||
| ```typescript | ||
| import { Generator } from '@janus-validator/core'; | ||
| import { O, U, I } from '@janus-validator/dsl'; | ||
| const User = O({ name: U(1, 50), age: I(0, 150) }); | ||
| const generator = new Generator({ random: Math.random }); | ||
| const fixture = generator.generate(User); | ||
| const roundTrip = User.validate(fixture); | ||
| // roundTrip.valid === true | ||
| ``` | ||
| ## DSL Reference | ||
@@ -136,2 +159,51 @@ | ||
| ## Recipes | ||
| ### 1) Nested objects (strict vs non-strict) | ||
| ```typescript | ||
| import { O, U, I } from '@janus-validator/dsl'; | ||
| const User = O({ | ||
| name: U(1, 100), | ||
| age: I(0, 150), | ||
| }); | ||
| const StrictUser = O({ name: U(1, 100), age: I(0, 150) }, true); | ||
| ``` | ||
| ### 2) “Enum-like” values (auto-wrapping) | ||
| ```typescript | ||
| import { Or } from '@janus-validator/dsl'; | ||
| const Status = Or('pending', 'active', 'complete'); | ||
| // Type is: Validator<'pending' | 'active' | 'complete'> | ||
| ``` | ||
| ### 3) Formatted strings without regex | ||
| ```typescript | ||
| import { S, D, H } from '@janus-validator/dsl'; | ||
| const ISODate = S(D(4), '-', D(2), '-', D(2)); // YYYY-MM-DD | ||
| const UUID = S(H(8), '-', H(4), '-', H(4), '-', H(4), '-', H(12)); | ||
| ``` | ||
| ### 4) Capture & reference (e.g. password confirmation) | ||
| ```typescript | ||
| import { O, U, createCaptureGroup } from '@janus-validator/dsl'; | ||
| const { capture, ref, context } = createCaptureGroup(); | ||
| const Signup = O({ | ||
| password: capture('pwd', U(8, 100)), | ||
| confirmPassword: ref('pwd'), | ||
| }); | ||
| // If reusing between validations, clear captures | ||
| context.clear(); | ||
| ``` | ||
| ## Auto-Wrapping | ||
@@ -138,0 +210,0 @@ |
50311
3.48%225
47.06%888
-0.22%