Skip to content

Commit

Permalink
Support UUIDs in JSON Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
schani committed Jul 12, 2018
1 parent dd2dd2d commit 61cc926
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/quicktype-core/StringTypes.ts
Expand Up @@ -177,6 +177,12 @@ function isIntegerString(s: string): boolean {
return i >= MIN_INTEGER_STRING && i <= MAX_INTEGER_STRING;
}

const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;

function isUUID(s: string): boolean {
return s.match(UUID) !== null;
}

/**
* JSON inference calls this function to figure out whether a given string is to be
* transformed into a higher level type. Must return undefined if not, otherwise the
Expand All @@ -185,7 +191,7 @@ function isIntegerString(s: string): boolean {
* @param s The string for which to determine the transformed string type kind.
*/
export function inferTransformedStringTypeKindForString(s: string): TransformedStringTypeKind | undefined {
if (s.length === 0 || "0123456789-ft".indexOf(s[0]) < 0) return undefined;
if (s.length === 0 || "0123456789-abcdeft".indexOf(s[0]) < 0) return undefined;

if (isDate(s)) {
return "date";
Expand All @@ -197,6 +203,8 @@ export function inferTransformedStringTypeKindForString(s: string): TransformedS
return "integer-string";
} else if (s === "false" || s === "true") {
return "bool-string";
} else if (isUUID(s)) {
return "uuid";
}
return undefined;
}
1 change: 1 addition & 0 deletions src/quicktype-core/Type.ts
Expand Up @@ -51,6 +51,7 @@ export const transformedStringTypeTargetTypeKinds = {
date: { jsonSchema: "date", primitive: undefined },
time: { jsonSchema: "time", primitive: undefined },
"date-time": { jsonSchema: "date-time", primitive: undefined },
uuid: { jsonSchema: "uuid", primitive: undefined },
"integer-string": { jsonSchema: "integer", primitive: "integer" } as TransformedStringTypeTargets,
"bool-string": { jsonSchema: "boolean", primitive: "bool" } as TransformedStringTypeTargets
};
Expand Down
8 changes: 8 additions & 0 deletions test/inputs/schema/uuid.1.json
@@ -0,0 +1,8 @@
{
"one": "68ffea67-509e-482e-90ca-e80e5709cd78",
"optional": "891f6132-9749-4907-bd52-9d50ca47fe3a",
"nullable": "22dc9756-bb07-4bdd-b975-700b23e8d3ca",
"arrOne": ["a89b1ff1-2515-46ae-ae9b-bf2e278f63ea", "ac4095ec-1e94-4c2a-9180-8fd71bfcc0f6"],
"arrNullable": ["5cb0e36e-641e-4e12-bf75-dfc9d489ea49", null, "439ccde9-27a6-44b9-b195-cf155aeab732"],
"unionWithEnum": "a5004f81-17ea-4c2f-b652-262e4b103ad6"
}
5 changes: 5 additions & 0 deletions test/inputs/schema/uuid.2.json
@@ -0,0 +1,5 @@
{
"one": "68ffea67-509e-482e-90ca-e80e5709cd78",
"nullable": null,
"unionWithEnum": "foo"
}
43 changes: 43 additions & 0 deletions test/inputs/schema/uuid.schema
@@ -0,0 +1,43 @@
{
"type": "object",
"properties": {
"one": {
"type": "string",
"format": "uuid"
},
"optional": {
"$ref": "#/properties/one"
},
"nullable": {
"oneOf": [
{
"type": "null"
},
{
"$ref": "#/properties/one"
}
]
},
"arrOne": {
"type": "array",
"items": {
"$ref": "#/properties/one"
}
},
"arrNullable": {
"type": "array",
"items": {
"$ref": "#/properties/nullable"
}
},
"unionWithEnum": {
"oneOf": [
{
"$ref": "#/properties/one"
},
{ "enum": ["foo"] }
]
}
},
"required": ["one", "nullable", "unionWithEnum"]
}

0 comments on commit 61cc926

Please sign in to comment.