Skip to content

Commit 9136aaa

Browse files
authored
Version 1.0.39 (#1411)
* Add Anchor, DynamicAnchor, DynamicRef Guards * ChangeLog * Version
1 parent de0939c commit 9136aaa

9 files changed

Lines changed: 210 additions & 11 deletions

File tree

changelog/1.0.0.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
---
44

55
### Version Updates
6+
- [Revision 1.0.39](https://github.com/sinclairzx81/typebox/pull/1411)
7+
- Add IsAnchor, IsDynamicAnchor and IsDynamicRef Guards
68
- [Revision 1.0.38](https://github.com/sinclairzx81/typebox/pull/1410)
79
- Json Schema Inference: Progressive Constraint Inference for Items and PrefixItems
810
- [Revision 1.0.37](https://github.com/sinclairzx81/typebox/pull/1406)
@@ -182,15 +184,11 @@ const X = Compile(Type.Script(`1 | 2 | 3`))
182184

183185
const Y = Compile({ anyOf: [{ const: 1 }, { const: 2 }, { const: 3 }] })
184186

185-
const Z = Compile(z.union([z.literal(1), z.literal(2), z.literal(3)])) // Zod
186-
187187
// Unified Parse and Inference
188188

189189
const A = X.Parse(1) // const A: 1 | 2 | 3
190190

191191
const B = Y.Parse(2) // const B: 1 | 2 | 3
192-
193-
const C = Z.Parse(3) // const C: 1 | 2 | 3
194192
```
195193

196194

src/schema/types/anchor.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*--------------------------------------------------------------------------
2+
3+
TypeBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2017-2025 Haydn Paterson
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
---------------------------------------------------------------------------*/
28+
29+
// deno-fmt-ignore-file
30+
31+
import { Guard } from '../../guard/index.ts'
32+
import { type XSchemaObject } from './schema.ts'
33+
34+
// ------------------------------------------------------------------
35+
// Type
36+
// ------------------------------------------------------------------
37+
export interface XAnchor<Anchor extends string = string> {
38+
$anchor: Anchor
39+
}
40+
// ------------------------------------------------------------------
41+
// Guard
42+
// ------------------------------------------------------------------
43+
/**
44+
* Returns true if the schema contains a valid $anchor property
45+
*/
46+
export function IsAnchor(schema: XSchemaObject): schema is XAnchor {
47+
return Guard.HasPropertyKey(schema, '$anchor')
48+
&& Guard.IsString(schema.$anchor)
49+
}

src/schema/types/dynamicAnchor.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*--------------------------------------------------------------------------
2+
3+
TypeBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2017-2025 Haydn Paterson
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
---------------------------------------------------------------------------*/
28+
29+
// deno-fmt-ignore-file
30+
31+
import { Guard } from '../../guard/index.ts'
32+
import { type XSchemaObject } from './schema.ts'
33+
34+
// ------------------------------------------------------------------
35+
// Type
36+
// ------------------------------------------------------------------
37+
export interface XDynamicAnchor<Anchor extends string = string> {
38+
$dynamicAnchor: Anchor
39+
}
40+
// ------------------------------------------------------------------
41+
// Guard
42+
// ------------------------------------------------------------------
43+
/**
44+
* Returns true if the schema contains a valid $dynamicAnchor property
45+
*/
46+
export function IsDynamicAnchor(schema: XSchemaObject): schema is XDynamicAnchor {
47+
return Guard.HasPropertyKey(schema, '$dynamicAnchor')
48+
&& Guard.IsString(schema.$dynamicAnchor)
49+
}

src/schema/types/dynamicRef.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*--------------------------------------------------------------------------
2+
3+
TypeBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2017-2025 Haydn Paterson
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
---------------------------------------------------------------------------*/
28+
29+
// deno-fmt-ignore-file
30+
31+
import { Guard } from '../../guard/index.ts'
32+
import { type XSchemaObject } from './schema.ts'
33+
34+
// ------------------------------------------------------------------
35+
// Type
36+
// ------------------------------------------------------------------
37+
export interface XDynamicRef<Ref extends string = string> {
38+
$dynamicRef: Ref
39+
}
40+
// ------------------------------------------------------------------
41+
// Guard
42+
// ------------------------------------------------------------------
43+
/**
44+
* Returns true if the schema contains a valid $dynamicRef property
45+
*/
46+
export function IsDynamicRef(schema: XSchemaObject): schema is XDynamicRef {
47+
return Guard.HasPropertyKey(schema, '$dynamicRef')
48+
&& Guard.IsString(schema.$dynamicRef)
49+
}

src/schema/types/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ export * from './_refine.ts'
3535
// ------------------------------------------------------------------
3636
// Standard
3737
// ------------------------------------------------------------------
38-
export * from './additionalProperties.ts'
3938
export * from './additionalItems.ts'
39+
export * from './additionalProperties.ts'
4040
export * from './allOf.ts'
41+
export * from './anchor.ts'
4142
export * from './anyOf.ts'
4243
export * from './const.ts'
4344
export * from './contains.ts'
@@ -46,6 +47,10 @@ export * from './contentMediaType.ts'
4647
export * from './default.ts'
4748
export * from './defs.ts'
4849
export * from './dependencies.ts'
50+
export * from './dependentRequired.ts'
51+
export * from './dependentSchemas.ts'
52+
export * from './dynamicAnchor.ts'
53+
export * from './dynamicRef.ts'
4954
export * from './else.ts'
5055
export * from './enum.ts'
5156
export * from './exclusiveMaximum.ts'
@@ -55,10 +60,12 @@ export * from './id.ts'
5560
export * from './if.ts'
5661
export * from './items.ts'
5762
export * from './maximum.ts'
63+
export * from './maxContains.ts'
5864
export * from './maxItems.ts'
5965
export * from './maxLength.ts'
6066
export * from './maxProperties.ts'
6167
export * from './minimum.ts'
68+
export * from './minContains.ts'
6269
export * from './minItems.ts'
6370
export * from './minLength.ts'
6471
export * from './minProperties.ts'
@@ -73,13 +80,9 @@ export * from './propertyNames.ts'
7380
export * from './ref.ts'
7481
export * from './required.ts'
7582
export * from './schema.ts'
83+
export * from './schema.ts'
7684
export * from './then.ts'
7785
export * from './type.ts'
7886
export * from './uniqueItems.ts'
79-
export * from './dependentRequired.ts'
80-
export * from './dependentSchemas.ts'
81-
export * from './minContains.ts'
82-
export * from './maxContains.ts'
8387
export * from './unevaluatedItems.ts'
8488
export * from './unevaluatedProperties.ts'
85-
export * from './schema.ts'

tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Range } from './task/range/index.ts'
88
import { Metrics } from './task/metrics/index.ts'
99
import { Task } from 'tasksmith'
1010

11-
const Version = '1.0.38'
11+
const Version = '1.0.39'
1212

1313
// ------------------------------------------------------------------
1414
// Build
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Schema from 'typebox/schema'
2+
import { Assert } from 'test'
3+
4+
const Test = Assert.Context('Schema.IsAnchor')
5+
6+
Test('Should Guard 1', () => {
7+
Assert.IsTrue(Schema.IsAnchor({ $anchor: 'my-ref' }))
8+
})
9+
Test('Should Guard 2', () => {
10+
Assert.IsFalse(Schema.IsAnchor({ $anchor: 123 }))
11+
})
12+
Test('Should Guard 3', () => {
13+
Assert.IsFalse(Schema.IsAnchor({ $anchor: null }))
14+
})
15+
Test('Should Guard 4', () => {
16+
Assert.IsFalse(Schema.IsAnchor({}))
17+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Schema from 'typebox/schema'
2+
import { Assert } from 'test'
3+
4+
const Test = Assert.Context('Schema.IsDynamicAnchor')
5+
6+
Test('Should Guard 1', () => {
7+
Assert.IsTrue(Schema.IsDynamicAnchor({ $dynamicAnchor: 'my-ref' }))
8+
})
9+
Test('Should Guard 2', () => {
10+
Assert.IsFalse(Schema.IsDynamicAnchor({ $dynamicAnchor: 123 }))
11+
})
12+
Test('Should Guard 3', () => {
13+
Assert.IsFalse(Schema.IsDynamicAnchor({ $dynamicAnchor: null }))
14+
})
15+
Test('Should Guard 4', () => {
16+
Assert.IsFalse(Schema.IsDynamicAnchor({}))
17+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Schema from 'typebox/schema'
2+
import { Assert } from 'test'
3+
4+
const Test = Assert.Context('Schema.IsDynamicRef')
5+
6+
Test('Should Guard 1', () => {
7+
Assert.IsTrue(Schema.IsDynamicRef({ $dynamicRef: 'my-ref' }))
8+
})
9+
Test('Should Guard 2', () => {
10+
Assert.IsFalse(Schema.IsDynamicRef({ $dynamicRef: 123 }))
11+
})
12+
Test('Should Guard 3', () => {
13+
Assert.IsFalse(Schema.IsDynamicRef({ $dynamicRef: null }))
14+
})
15+
Test('Should Guard 4', () => {
16+
Assert.IsFalse(Schema.IsDynamicRef({}))
17+
})

0 commit comments

Comments
 (0)