Skip to content

Commit

Permalink
feat: add support for type URL (string with format "uri") (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaswr committed Nov 22, 2022
1 parent 2ed8ab1 commit 2ba0135
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fs.writeFile(output_path, schemaString, (err) => {
- `interface` types
- `enum` types
- `union`, `tuple`, `type[]` types
- `Date`, `RegExp` types
- `Date`, `RegExp`, `URL` types
- `string`, `boolean`, `number` types
- `"value"`, `123`, `true`, `false`, `null`, `undefined` literals
- type aliases
Expand Down
4 changes: 4 additions & 0 deletions src/NodeParser/TypeReferenceNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export class TypeReferenceNodeParser implements SubNodeParser {
return new AnnotatedType(new StringType(), { format: "regex" }, false);
}

if (typeSymbol.name === "URL") {
return new AnnotatedType(new StringType(), { format: "uri" }, false);
}

return this.childNodeParser.createType(
typeSymbol.declarations!.filter((n: ts.Declaration) => !invalidTypes[n.kind])[0],
this.createSubContext(node, context)
Expand Down
1 change: 1 addition & 0 deletions test/valid-data-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe("valid-data-type", () => {
it("type-date", assertValidSchema("type-date", "MyObject"));
it("type-date-annotation", assertValidSchema("type-date-annotation", "MyObject", "basic"));
it("type-regexp", assertValidSchema("type-regexp", "MyObject"));
it("type-uri", assertValidSchema("type-uri", "MyObject"));
it("type-union", assertValidSchema("type-union", "TypeUnion"));
it("type-union-tagged", assertValidSchema("type-union-tagged", "Shape"));
it("type-intersection", assertValidSchema("type-intersection", "MyObject"));
Expand Down
6 changes: 6 additions & 0 deletions test/valid-data/type-uri/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type MyURL = URL

export interface MyObject {
url: URL;
urlAlias: MyURL;
}
24 changes: 24 additions & 0 deletions test/valid-data/type-uri/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"additionalProperties": false,
"properties": {
"url": {
"format": "uri",
"type": "string"
},
"urlAlias": {
"format": "uri",
"type": "string"
}
},
"required": [
"url",
"urlAlias"
],
"type": "object"
}
}
}

0 comments on commit 2ba0135

Please sign in to comment.