Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ A list of available block attributes, and whether they can be used in pattern ma

## Document type variants

Document type variants allow documents to use a suffixed type like `"core/article+template"` and still match the base declaration `"core/article"`. Variants are configured on the validator, not in constraint sets, so that the set of allowed suffixes is controlled by the application.
Document type variants allow documents to use a suffixed type like `"core/article#template"` and still match the base declaration `"core/article"`. Variants are configured on the validator, not in constraint sets, so that the set of allowed suffixes is controlled by the application.

Configure variants using `WithVariants`:

Expand All @@ -396,7 +396,7 @@ if err != nil {
log.Fatal(err)
}

// Allow "+template" for all declared document types.
// Allow "#template" for all declared document types.
validator = validator.WithVariants(revisor.Variant{
Name: "template",
})
Expand All @@ -421,7 +421,11 @@ Variants are preserved across `WithConstraints` calls.

Revisor implements a file-driven test in `TestValidateDocument` that checks so that all the "testdata/results/*.json" files match the validation results for the corresponding document under "testdata/". Result files with the prefix "base-" will be validated against "constraints/naviga.json", for result files with the prefix "example-" the "constraints/example.json" constraints will be used as well.

If the constraints have been updated, or new example documents have been added, the result files can be regenerated using `./update-test-results.sh`.
If the constraints have been updated, or new example documents have been added, the result files can be regenerated by running the tests with the `REGENERATE` environment variable set:

```
REGENERATE=true go test -run 'TestValidateDocument|TestCollection|TestDeprecation' ./...
```

### Benchmarks

Expand Down
8 changes: 4 additions & 4 deletions document_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,19 @@ func documentAttribute(d *newsdoc.Document, name string) (string, bool) {
}

// Variant defines a document type variant suffix. When a document type
// contains a "+" separator (e.g. "core/article+template"), the part after the
// last "+" is matched against configured variants. If Types is empty, the
// contains a "#" separator (e.g. "core/article#template"), the part after the
// last "#" is matched against configured variants. If Types is empty, the
// variant applies to all declared document types.
type Variant struct {
Name string `json:"name"`
Types []string `json:"types,omitempty"`
}

// resolveVariant returns the base document type if the suffix after the last
// "+" matches a configured variant (and the base type is allowed for that
// "#" matches a configured variant (and the base type is allowed for that
// variant). Returns docType unchanged if no variant matches.
func resolveVariant(docType string, variants []Variant) string {
idx := strings.LastIndex(docType, "+")
idx := strings.LastIndex(docType, "#")
if idx == -1 {
return docType
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/example-article-template.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"uuid": "98cedf81-ce8d-4dc4-bcbc-6091a756cc5c",
"type": "core/article+template",
"type": "core/article#template",
"uri": "core://article/98cedf81-ce8d-4dc4-bcbc-6091a756cc5c",
"title": "champ-inter",
"content": [
Expand Down
2 changes: 1 addition & 1 deletion testdata/geo-info-template.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "core/place+template",
"type": "core/place#template",
"uuid": "d9a8a437-3ed3-4b97-b9fc-cf236b6dc5e7",
"title": "Some place",
"meta": [
Expand Down
14 changes: 0 additions & 14 deletions update-entities.sh

This file was deleted.

12 changes: 0 additions & 12 deletions update-test-results.sh

This file was deleted.

8 changes: 4 additions & 4 deletions validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func TestTemplateDocumentType(t *testing.T) {

t.Run("TemplateMatchesDeclares", func(t *testing.T) {
doc := newsdoc.Document{
Type: "core/article+template",
Type: "core/article#template",
UUID: "00000000-0000-0000-0000-000000000001",
URI: "article://test/1",
}
Expand All @@ -452,7 +452,7 @@ func TestTemplateDocumentType(t *testing.T) {

t.Run("UnsupportedSuffixRejected", func(t *testing.T) {
doc := newsdoc.Document{
Type: "core/article+other",
Type: "core/article#other",
UUID: "00000000-0000-0000-0000-000000000001",
URI: "article://test/1",
}
Expand All @@ -473,7 +473,7 @@ func TestTemplateDocumentType(t *testing.T) {
}

if !found {
t.Error("expected undeclared document type error for +other suffix")
t.Error("expected undeclared document type error for #other suffix")
}
})

Expand Down Expand Up @@ -505,7 +505,7 @@ func TestTemplateDocumentType(t *testing.T) {
)

doc := newsdoc.Document{
Type: "core/article+template",
Type: "core/article#template",
UUID: "00000000-0000-0000-0000-000000000001",
URI: "article://test/1",
}
Expand Down