Skip to content

Commit 6c8870e

Browse files
authored
Fully support allOf (#25)
Fixes: sourcemeta/jsonschema#619 Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 8ea705e commit 6c8870e

15 files changed

Lines changed: 347 additions & 1 deletion

File tree

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ to use a JSON Schema validator at runtime to enforce remaining constraints.
3636
| Applicator (2020-12) | `propertyNames` | Ignored |
3737
| Applicator (2020-12) | `dependentSchemas` | Pending |
3838
| Applicator (2020-12) | `contains` | Ignored |
39-
| Applicator (2020-12) | `allOf` | Pending |
39+
| Applicator (2020-12) | `allOf` | Yes |
4040
| Applicator (2020-12) | `oneOf` | **PARTIAL GIVEN LANGUAGE LIMITATIONS** |
4141
| Applicator (2020-12) | `not` | **CANNOT SUPPORT** |
4242
| Applicator (2020-12) | `if` | Pending |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export type AllOfIntersection_1Age = number;
2+
3+
export type AllOfIntersection_1AdditionalProperties = never;
4+
5+
export interface AllOfIntersection_1 {
6+
"age": AllOfIntersection_1Age;
7+
}
8+
9+
export type AllOfIntersection_0Name = string;
10+
11+
export type AllOfIntersection_0AdditionalProperties = never;
12+
13+
export interface AllOfIntersection_0 {
14+
"name": AllOfIntersection_0Name;
15+
}
16+
17+
export type AllOfIntersection =
18+
AllOfIntersection_0 &
19+
AllOfIntersection_1;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"defaultPrefix": "AllOfIntersection"
3+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"allOf": [
4+
{
5+
"type": "object",
6+
"properties": {
7+
"name": { "type": "string" }
8+
},
9+
"required": [ "name" ],
10+
"additionalProperties": false
11+
},
12+
{
13+
"type": "object",
14+
"properties": {
15+
"age": { "type": "integer" }
16+
},
17+
"required": [ "age" ],
18+
"additionalProperties": false
19+
}
20+
]
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { AllOfIntersection } from "./expected";
2+
3+
// Valid: satisfies both branches
4+
const valid: AllOfIntersection = {
5+
name: "Alice",
6+
age: 30
7+
};
8+
9+
// Invalid: missing age from second branch
10+
// @ts-expect-error
11+
const missingAge: AllOfIntersection = {
12+
name: "Bob"
13+
};
14+
15+
// Invalid: missing name from first branch
16+
// @ts-expect-error
17+
const missingName: AllOfIntersection = {
18+
age: 25
19+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export type Person_1 = PersonAged;
2+
3+
export type Person_0 = PersonNamed;
4+
5+
export type PersonNamedName = string;
6+
7+
export type PersonNamedAdditionalProperties = never;
8+
9+
export interface PersonNamed {
10+
"name": PersonNamedName;
11+
}
12+
13+
export type PersonAgedAge = number;
14+
15+
export type PersonAgedAdditionalProperties = never;
16+
17+
export interface PersonAged {
18+
"age": PersonAgedAge;
19+
}
20+
21+
export type Person =
22+
Person_0 &
23+
Person_1;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"defaultPrefix": "Person"
3+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$defs": {
4+
"Named": {
5+
"type": "object",
6+
"properties": {
7+
"name": { "type": "string" }
8+
},
9+
"required": [ "name" ],
10+
"additionalProperties": false
11+
},
12+
"Aged": {
13+
"type": "object",
14+
"properties": {
15+
"age": { "type": "integer" }
16+
},
17+
"required": [ "age" ],
18+
"additionalProperties": false
19+
}
20+
},
21+
"allOf": [
22+
{ "$ref": "#/$defs/Named" },
23+
{ "$ref": "#/$defs/Aged" }
24+
]
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Person } from "./expected";
2+
3+
// Valid: satisfies both $ref branches
4+
const valid: Person = {
5+
name: "Alice",
6+
age: 30
7+
};
8+
9+
// Invalid: missing age
10+
// @ts-expect-error
11+
const missingAge: Person = {
12+
name: "Bob"
13+
};
14+
15+
// Invalid: missing name
16+
// @ts-expect-error
17+
const missingName: Person = {
18+
age: 25
19+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type Wrapper_0Value = string;
2+
3+
export type Wrapper_0AdditionalProperties = never;
4+
5+
export interface Wrapper_0 {
6+
"value": Wrapper_0Value;
7+
}
8+
9+
export type Wrapper = Wrapper_0;

0 commit comments

Comments
 (0)