From 992e094365739d2e0d8031d9f273fd0f4b21729f Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Fri, 13 Oct 2023 18:22:04 +0300 Subject: [PATCH] don't limit the explicit format --- fixtures/with_custom_format.json | 31 +++++++++++++++++++++++++++++++ reflect.go | 5 +---- reflect_test.go | 12 ++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 fixtures/with_custom_format.json diff --git a/fixtures/with_custom_format.json b/fixtures/with_custom_format.json new file mode 100644 index 0000000..52fa9ed --- /dev/null +++ b/fixtures/with_custom_format.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://github.com/invopop/jsonschema/with-custom-format", + "$ref": "#/$defs/WithCustomFormat", + "$defs": { + "WithCustomFormat": { + "properties": { + "dates": { + "items": { + "type": "string", + "format": "date" + }, + "type": "array" + }, + "odds": { + "items": { + "type": "string", + "format": "odd" + }, + "type": "array" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "dates", + "odds" + ] + } + } +} \ No newline at end of file diff --git a/reflect.go b/reflect.go index 3249c8c..fae9f99 100644 --- a/reflect.go +++ b/reflect.go @@ -768,10 +768,7 @@ func (t *Schema) stringKeywords(tags []string) { case "pattern": t.Pattern = val case "format": - switch val { - case "date-time", "email", "hostname", "ipv4", "ipv6", "uri", "uuid": - t.Format = val - } + t.Format = val case "readOnly": i, _ := strconv.ParseBool(val) t.ReadOnly = i diff --git a/reflect_test.go b/reflect_test.go index 94b6018..abf5dbf 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -623,6 +623,18 @@ func TestUnsignedIntHandling(t *testing.T) { fixtureContains(t, "fixtures/unsigned_int_handling.json", `"maxItems": 0`) } +func TestJSONSchemaFormat(t *testing.T) { + type WithCustomFormat struct { + Dates []string `json:"dates" jsonschema:"format=date"` + Odds []string `json:"odds" jsonschema:"format=odd"` + } + + r := &Reflector{} + compareSchemaOutput(t, "fixtures/with_custom_format.json", r, &WithCustomFormat{}) + fixtureContains(t, "fixtures/with_custom_format.json", `"format": "date"`) + fixtureContains(t, "fixtures/with_custom_format.json", `"format": "odd"`) +} + type AliasObjectA struct { PropA string `json:"prop_a"` }