From 3e287503eb42270fd08fe6d2aeea8ee5e9cc529e Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sun, 29 Nov 2026 18:09:08 +0000 Subject: [PATCH] Bundle and validate the shared glossary contract --- docs/oss/attribution.md | 13 ++ docs/oss/licenses/CC0-1.0.txt | 121 ++++++++++++++++++ docs/roadmap/implementation.md | 4 +- .../assets/docs/compatibility.md | 3 + .../sharedglossary/assets/docs/validation.md | 3 + .../glossary/v0.2.0/examples/glossary.yml | 21 +++ .../glossary/v0.2.0/examples/sports.yml | 16 +++ .../glossary/v0.2.0/examples/weather.json | 12 ++ .../glossary/v0.2.0/examples/weather.yml | 15 +++ .../assets/glossary/v0.2.0/schema.json | 51 ++++++++ .../assets/glossary/v0.2.0/semantics.md | 83 ++++++++++++ internal/appconfig/sharedglossary/schema.go | 52 ++++++++ .../appconfig/sharedglossary/schema_test.go | 81 ++++++++++++ 13 files changed, 474 insertions(+), 1 deletion(-) create mode 100644 docs/oss/licenses/CC0-1.0.txt create mode 100644 internal/appconfig/sharedglossary/assets/docs/compatibility.md create mode 100644 internal/appconfig/sharedglossary/assets/docs/validation.md create mode 100644 internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/glossary.yml create mode 100644 internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/sports.yml create mode 100644 internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/weather.json create mode 100644 internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/weather.yml create mode 100644 internal/appconfig/sharedglossary/assets/glossary/v0.2.0/schema.json create mode 100644 internal/appconfig/sharedglossary/assets/glossary/v0.2.0/semantics.md create mode 100644 internal/appconfig/sharedglossary/schema.go create mode 100644 internal/appconfig/sharedglossary/schema_test.go diff --git a/docs/oss/attribution.md b/docs/oss/attribution.md index 510aa01..c5b04d8 100644 --- a/docs/oss/attribution.md +++ b/docs/oss/attribution.md @@ -258,3 +258,16 @@ License and notices: [BSD 3-Clause](licenses/BSD-3-Clause-Go.txt) | [LICENSE](https://github.com/spf13/pflag/blob/f8a990678a6d44385313e9b19e40737f889f9b14/LICENSE). [Source](https://github.com/spf13/pflag/tree/f8a990678a6d44385313e9b19e40737f889f9b14). + +## Maximum Direct shared schemas + +Material: `glossary/v0.2.0/` from schemas distribution `2026.11.1`. + +Copyright 2026 Eric Rakestraw + +SPDX-License-Identifier: CC0-1.0 + +License and notices: [CC0 1.0](licenses/CC0-1.0.txt) | +[COPYRIGHT](https://forge.maximumdirect.net/eric/schemas/src/commit/f7e3cedeca11ed903311cdf29ca4d81fb1f216b7/COPYRIGHT). + +[Source](https://forge.maximumdirect.net/eric/schemas/src/commit/f7e3cedeca11ed903311cdf29ca4d81fb1f216b7). diff --git a/docs/oss/licenses/CC0-1.0.txt b/docs/oss/licenses/CC0-1.0.txt new file mode 100644 index 0000000..0e259d4 --- /dev/null +++ b/docs/oss/licenses/CC0-1.0.txt @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index efd1435..906679b 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -1,6 +1,6 @@ # Shared Glossary Implementation Plan -**Status:** Planning only. Target release: Audita v4.3.0. +**Status:** In progress. Target release: Audita v4.3.0. Implement the [shared glossary roadmap](shared-glossary.md). It owns the desired behavior and user decisions. This plan owns implementation order and validation. @@ -61,6 +61,8 @@ remains. Stage 4 removes the old discovery path. ## Stage 1: Bundle and Validate the Selected Contract +**Completed.** + **Work** 1. Add an internal `internal/appconfig/sharedglossary` package for the file-boundary diff --git a/internal/appconfig/sharedglossary/assets/docs/compatibility.md b/internal/appconfig/sharedglossary/assets/docs/compatibility.md new file mode 100644 index 0000000..0c2cfaa --- /dev/null +++ b/internal/appconfig/sharedglossary/assets/docs/compatibility.md @@ -0,0 +1,3 @@ +# Upstream compatibility guidance + +See the [compatibility guide from the selected schemas distribution](https://forge.maximumdirect.net/eric/schemas/src/commit/f7e3cedeca11ed903311cdf29ca4d81fb1f216b7/docs/compatibility.md). diff --git a/internal/appconfig/sharedglossary/assets/docs/validation.md b/internal/appconfig/sharedglossary/assets/docs/validation.md new file mode 100644 index 0000000..092276a --- /dev/null +++ b/internal/appconfig/sharedglossary/assets/docs/validation.md @@ -0,0 +1,3 @@ +# Upstream validation guidance + +See the [validation guide from the selected schemas distribution](https://forge.maximumdirect.net/eric/schemas/src/commit/f7e3cedeca11ed903311cdf29ca4d81fb1f216b7/docs/validation.md). diff --git a/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/glossary.yml b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/glossary.yml new file mode 100644 index 0000000..39f804e --- /dev/null +++ b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/glossary.yml @@ -0,0 +1,21 @@ +schema_version: maximumdirect.glossary.v1 +collections: + campaign: + entries: + - id: lighthouse + name: Glass Lantern + category: place + aliases: [The Lantern] + description: A fictional lighthouse at the entrance to the bay. + spells: + provenance: + id: lantern-coast-original + title: Synthetic campaign spells + version: "1" + license: CC0-1.0 + entries: + - id: ember-veil + name: Ember Veil + category: spell + aliases: [Veil of Embers] + casting_time: one action diff --git a/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/sports.yml b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/sports.yml new file mode 100644 index 0000000..fb6732c --- /dev/null +++ b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/sports.yml @@ -0,0 +1,16 @@ +schema_version: maximumdirect.glossary.v1 +collections: + clubs: + sport: association football + entries: + - id: harbour + name: Harbour Wanderers + category: club + aliases: [] + plural: null + home_ground: Lantern Park + venues: + entries: + - id: harbour + name: Lantern Park + category: venue diff --git a/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/weather.json b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/weather.json new file mode 100644 index 0000000..b81631a --- /dev/null +++ b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/weather.json @@ -0,0 +1,12 @@ +{ + "schema_version": "maximumdirect.glossary.v1", + "collections": { + "measurements": { + "unit_system": "metric", + "entries": [ + {"id": "temperature", "name": "Air temperature", "category": "measurement", "aliases": ["Temperature"], "unit": "degree Celsius"}, + {"id": "precipitation", "name": "Precipitation", "category": "measurement", "aliases": null, "unit": "millimetre"} + ] + } + } +} diff --git a/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/weather.yml b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/weather.yml new file mode 100644 index 0000000..d06041c --- /dev/null +++ b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/examples/weather.yml @@ -0,0 +1,15 @@ +schema_version: maximumdirect.glossary.v1 +collections: + measurements: + unit_system: metric + entries: + - id: temperature + name: Air temperature + category: measurement + aliases: [Temperature] + unit: degree Celsius + - id: precipitation + name: Precipitation + category: measurement + aliases: null + unit: millimetre diff --git a/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/schema.json b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/schema.json new file mode 100644 index 0000000..c5121ca --- /dev/null +++ b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/schema.json @@ -0,0 +1,51 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://schemas.maximumdirect.net/glossary/v0.2.0/schema.json", + "title": "MAXIMUMDIRECT Glossary v1", + "type": "object", + "required": ["schema_version", "collections"], + "properties": { + "schema_version": {"const": "maximumdirect.glossary.v1"}, + "collections": { + "type": "object", + "propertyNames": {"minLength": 1}, + "additionalProperties": {"$ref": "#/$defs/collection"} + } + }, + "$defs": { + "text": {"type": "string", "minLength": 1}, + "aliases": {"type": ["array", "null"], "items": {"$ref": "#/$defs/text"}}, + "entry": { + "type": "object", + "required": ["id", "name", "category"], + "properties": { + "id": {"$ref": "#/$defs/text"}, + "name": {"$ref": "#/$defs/text"}, + "category": {"$ref": "#/$defs/text"}, + "description": {"type": "string"}, + "aliases": {"$ref": "#/$defs/aliases"}, + "plural": {"type": ["string", "null"], "minLength": 1} + } + }, + "provenance": { + "type": "object", + "properties": { + "id": {"$ref": "#/$defs/text"}, + "title": {"$ref": "#/$defs/text"}, + "version": {"type": "string"}, + "url": {"type": "string"}, + "license": {"type": "string"} + } + }, + "partialCollection": { + "type": "object", + "properties": { + "entries": {"type": "array", "items": {"$ref": "#/$defs/entry"}}, + "provenance": {"$ref": "#/$defs/provenance"} + } + }, + "collection": { + "allOf": [{"$ref": "#/$defs/partialCollection"}, {"required": ["entries"]}] + } + } +} diff --git a/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/semantics.md b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/semantics.md new file mode 100644 index 0000000..b9f66df --- /dev/null +++ b/internal/appconfig/sharedglossary/assets/glossary/v0.2.0/semantics.md @@ -0,0 +1,83 @@ +# Glossary contract + +## Native loading boundary + +Full conformance includes acquisition through the public mapping-root handoff of +[ConfigKit v1.4.0](https://forge.maximumdirect.net/base-layer/configkit/src/commit/4a7ae664a6287d71cba20f59c7c5b37b6f05b0a3/docs/config.md), +followed by this schema and its semantic checks. ConfigKit owns syntax, structural +controls, numeric binding, and resource limits, including its 8 MiB source limit. +Dynamic integers bind as int64 and fractional/exponent values as finite float64; +authored decimal precision is not guaranteed. Preserve supported resolved values. +`extends` is structural input and source-root `config` is a control namespace; +nested ordinary `config` follows upstream rules. Other supported domain extensions +remain permitted. Schema validation adds no trimming, case folding, or defaults. +Structural validation alone does not prove native loading compatibility. The +pinned upstream contract defines this version's boundary independently of later +changes to the repository's general guidance. + +Contract: `maximumdirect.glossary.v1`. [Schema](schema.json). +The [validation profile](../../docs/validation.md) and [compatibility rules](../../docs/compatibility.md) +apply. Start with [campaign vocabulary](examples/glossary.yml), +[weather](examples/weather.yml), or [sports](examples/sports.yml). + +| Field | Required | Meaning | +| --- | --- | --- | +| `schema_version` | Yes | Exactly `maximumdirect.glossary.v1`. | +| `collections` | Yes | Mapping of nonempty collection names to collections; may be empty. | +| `collections..entries` | Yes | List of entries; may be empty. | +| `entries[].id` | Yes | Stable, nonempty identity unique within this collection. | +| `entries[].name` | Yes | Nonempty canonical display name. | +| `entries[].category` | Yes | Nonempty domain-defined category. | +| `entries[].description` | No | Description; an empty string is valid. | +| `entries[].aliases` | No | Alternative names for the same entity. May be null, empty, or a list of nonempty strings. | +| `entries[].plural` | No | Nonempty plural display form, or null. | +| `collections..provenance` | No | Source metadata object, possibly empty. Optional `id` and `title` are nonempty strings; optional `version`, `url`, and `license` are strings that may be empty. | + +All objects permit extensions. Optional fields accept omission; only `aliases` +and `plural` additionally accept null. Supported resolved values are preserved; schema validation adds no trimming, +case folding, coercion, or defaults. IDs and names compare exactly. Distinct +collections can reuse an ID. Display names, aliases, and categories need not be +unique. Lists retain their authored order but establish no precedence or execution +order. Collection names and categories are strings chosen by the domain, not a +fixed set of D&D labels. + +For example, `spells` entries contain names, aliases, and optionally extended +domain information such as `casting_time`. Their collection's provenance can +identify a source catalog. Domain-specific metadata can use extension fields. +Provenance describes origin and applicability; +it is not transcript evidence or a spell rule. If different source contexts +matter, use separately named collections with their own provenance. A consumer +can derive its recognition input from these entries instead of requiring a second +operator-maintained spell list. Embedded vocabulary, source precedence, +normalization, inference, and conflicts remain consumer policy. + +Entry-ID uniqueness is a semantic rule checked separately from JSON Schema. +The [validator](../../docs/validation.md) performs both checks. No cross-document identity or +reference-resolution check is part of this initial contract. + +## Extensions and task instructions + +Extensions describe domain information. Consumers forwarding a shared document +should retain them; task-specific projections may intentionally omit them. +Acceptance and preservation do not claim interpretation. Defined fields retain +their schema constraints in an extended document. + +Task instructions belong to consumer configuration. `instruction` is not a +defined field in this contract. Like other unknown fields, it can be retained +without acquiring instruction semantics. Maintained examples contain domain +data only. + +## Authoring contributions + +`#/$defs/partialCollection` is an editor-assistance definition with optional +`entries`. `#/$defs/entry` and `#/$defs/collection` assist entry and complete +collection editing. A partial contribution is not a standalone valid domain +document. ConfigKit owns imports, inheritance, and composition before validation +of the complete envelope. + +## Differences from the reference catalog and Audita v4.2 + +The old format maps collection names directly at the root. The shared format +requires `schema_version` and `collections`. It removes defined collection +instructions, permits structural extensions, and adds defined provenance. +Existing consumers must explicitly adopt this envelope and its semantics. diff --git a/internal/appconfig/sharedglossary/schema.go b/internal/appconfig/sharedglossary/schema.go new file mode 100644 index 0000000..fead4f6 --- /dev/null +++ b/internal/appconfig/sharedglossary/schema.go @@ -0,0 +1,52 @@ +// Package sharedglossary implements the boundary for shared glossary documents. +package sharedglossary + +import ( + _ "embed" + "encoding/json" + "fmt" + + "github.com/santhosh-tekuri/jsonschema/v6" +) + +const schemaID = "https://schemas.maximumdirect.net/glossary/v0.2.0/schema.json" + +//go:embed assets/glossary/v0.2.0/schema.json +var schemaData []byte + +// Validator validates completed shared glossary envelopes. +type Validator struct { + schema *jsonschema.Schema +} + +// NewValidator compiles the locally bundled schema for reuse. +func NewValidator() (*Validator, error) { + var document any + if err := json.Unmarshal(schemaData, &document); err != nil { + return nil, fmt.Errorf("decode shared glossary schema: %w", err) + } + compiler := jsonschema.NewCompiler() + compiler.UseLoader(offlineLoader{}) + if err := compiler.AddResource(schemaID, document); err != nil { + return nil, fmt.Errorf("register shared glossary schema: %w", err) + } + schema, err := compiler.Compile(schemaID) + if err != nil { + return nil, fmt.Errorf("compile shared glossary schema: %w", err) + } + return &Validator{schema: schema}, nil +} + +// Validate checks an envelope without modifying it, retaining source context. +func (v *Validator) Validate(document map[string]any, source string) error { + if err := v.schema.Validate(document); err != nil { + return fmt.Errorf("shared glossary %s: %w", source, err) + } + return nil +} + +type offlineLoader struct{} + +func (offlineLoader) Load(url string) (any, error) { + return nil, fmt.Errorf("unregistered schema resource %q", url) +} diff --git a/internal/appconfig/sharedglossary/schema_test.go b/internal/appconfig/sharedglossary/schema_test.go new file mode 100644 index 0000000..a5f58e3 --- /dev/null +++ b/internal/appconfig/sharedglossary/schema_test.go @@ -0,0 +1,81 @@ +package sharedglossary + +import ( + "encoding/json" + "os" + "path/filepath" + "reflect" + "strings" + "testing" + + "go.yaml.in/yaml/v4" +) + +func TestValidate(t *testing.T) { + validator, err := NewValidator() + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + name, document, location string + valid bool + }{ + {"ordinary", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{"entries":[{"id":"one","name":"One","category":"number"}]}}}`, "", true}, + {"empty collections", `{"schema_version":"maximumdirect.glossary.v1","collections":{}}`, "", true}, + {"nullable and extensions", `{"schema_version":"maximumdirect.glossary.v1","extra":true,"collections":{"terms":{"instruction":42,"provenance":{"extra":true},"entries":[{"id":"one","name":"One","category":"number","aliases":null,"plural":null,"extra":{"n":1}}]}}}`, "", true}, + {"missing marker", `{"collections":{}}`, "schema_version", false}, + {"wrong marker", `{"schema_version":"wrong","collections":{}}`, "schema_version", false}, + {"missing entries", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{}}}`, "entries", false}, + {"invalid name", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{"entries":[{"id":"one","name":3,"category":"number"}]}}}`, "name", false}, + {"invalid aliases", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{"entries":[{"id":"one","name":"One","category":"number","aliases":3}]}}}`, "aliases", false}, + } { + t.Run(tc.name, func(t *testing.T) { + var document, before map[string]any + if err := json.Unmarshal([]byte(tc.document), &document); err != nil { + t.Fatal(err) + } + if err := json.Unmarshal([]byte(tc.document), &before); err != nil { + t.Fatal(err) + } + err := validator.Validate(document, "example.yml") + if (err == nil) != tc.valid { + t.Fatalf("Validate() = %v", err) + } + if !tc.valid && (!strings.Contains(err.Error(), "example.yml") || !strings.Contains(err.Error(), tc.location)) { + t.Fatalf("error lacks source or field location: %v", err) + } + if !reflect.DeepEqual(document, before) { + t.Fatal("validation modified the document") + } + }) + } +} + +func TestBundledExamples(t *testing.T) { + validator, err := NewValidator() + if err != nil { + t.Fatal(err) + } + for _, name := range []string{"glossary.yml", "sports.yml", "weather.yml", "weather.json"} { + t.Run(name, func(t *testing.T) { + path := filepath.Join("assets", "glossary", "v0.2.0", "examples", name) + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + var document map[string]any + if err := yaml.Unmarshal(data, &document); err != nil { + t.Fatal(err) + } + if err := validator.Validate(document, path); err != nil { + t.Fatal(err) + } + }) + } +} + +func TestOfflineLoader(t *testing.T) { + if _, err := (offlineLoader{}).Load("https://example.invalid/schema.json"); err == nil { + t.Fatal("unregistered resource was accepted") + } +}