Permalink
Browse files

Support UUIDs in JSON Schema

  • Loading branch information...
schani committed Jul 12, 2018
1 parent dd2dd2d commit 61cc926758352090ccfbfec425a9585a0ebb1555
@@ -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
@@ -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";
@@ -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;
}
@@ -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
};
@@ -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"
}
@@ -0,0 +1,5 @@
{
"one": "68ffea67-509e-482e-90ca-e80e5709cd78",
"nullable": null,
"unionWithEnum": "foo"
}
@@ -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.