feat(analyzer): a const says what type a property has - #118
Merged
Conversation
A property declaring const and no type resolved to any, though the const
value states the type exactly: 3.1 pins a discriminator that way, writing
kind: {const: dog} where a 3.0 spec wrote enum: [dog], so the tag of a
discriminated union came out untyped.
A scalar const now types the property, and a schema that states its own type
keeps it. A const that is not a scalar still says nothing a Go type carries
on its own.
if/then/else is dropped for the same reason dependentSchemas is, a shape
that depends on a value is a validation rule rather than a type, and now
warns like its sibling instead of vanishing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two JSON Schema 2020-12 keywords, both reached only through 3.1.
const
The const value states the type exactly, and 3.1 pins a discriminator this way:
kind: {const: dog}is what a 3.0 spec wrote asenum: [dog]. So the tag of a discriminated union, the one property a caller reads to know which variant they have, came out asany.Scalars map by their value: string, integer, number, boolean. A schema that states its own type keeps it, and a const that is not a scalar still resolves to
any, since it says nothing a single Go type carries.if / then / else
Dropped in silence, while
dependentSchemas, which is the same kind of keyword, warns:Not generated, for the reason in that sentence, and listed in the README's "Not supported" table beside its sibling.
Scale
Neither appears in the 3.1 specs I have to hand: ACTIVATE, its ledger service, and Mealie all use zero. This is correctness for the schema language 3.1 uses rather than a fix with a measured payoff, and the
consthalf matters most for specs that write discriminators the 3.1 way.Tests
internal/analyzer/schemas_const_test.go: each scalar const implies its type, a stated type wins, a non-scalar const staysany, andif/then/elsewarns exactly once per spec.gofmt,golangci-lint,go vet ./..., andgo test ./...pass.