Skip to content

Commit

Permalink
validation: allow numbers to be keyed locations
Browse files Browse the repository at this point in the history
While it's not a good idea to use a floating point as a key, they do
come up and are allowed by other important systems (such as PostgreSQL).

We should support them too.
  • Loading branch information
jgraettinger committed Jan 24, 2025
1 parent b442fee commit 6622e0e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions crates/json/src/schema/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ impl Set {
///
/// assert!(STRING.is_keyable_type());
/// assert!(INTEGER.is_keyable_type());
/// assert!(!FRACTIONAL.is_keyable_type());
/// assert!(FRACTIONAL.is_keyable_type());
/// assert!(BOOLEAN.is_keyable_type());
/// assert!(!INT_OR_FRAC.is_keyable_type());
/// assert!(INT_OR_FRAC.is_keyable_type());
/// assert!((STRING | NULL).is_keyable_type());
///
/// assert!(!(NULL.is_keyable_type()));
Expand All @@ -289,7 +289,7 @@ impl Set {
/// ```
pub fn is_keyable_type(&self) -> bool {
match *self - NULL {
BOOLEAN | INTEGER | STRING => true,
BOOLEAN | INTEGER | INT_OR_FRAC | FRACTIONAL | STRING => true,
_ => false,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/validation/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub enum Error {
PtrRegexUnmatched { ptr: String, unmatched: String },
#[error("location {ptr} is prohibited from ever existing by the schema {schema}")]
PtrCannotExist { ptr: String, schema: Url },
#[error("location {ptr} accepts {type_:?} in schema {schema}, but locations used as keys may only be null-able integers, strings, or booleans")]
#[error("location {ptr} accepts {type_:?} in schema {schema}, but locations used as keys may only be null-able numbers, strings, or booleans")]
KeyWrongType {
ptr: String,
type_: types::Set,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ expression: errors
[
Error {
scope: test://example/int-halve#/collections/testing~1int-halve/key/0,
error: location /int accepts "number", "object" in schema test://example/canonical/int-string-len.schema, but locations used as keys may only be null-able integers, strings, or booleans,
error: location /int accepts "number", "object" in schema test://example/canonical/int-string-len.schema, but locations used as keys may only be null-able numbers, strings, or booleans,
},
Error {
scope: test://example/int-reverse#/collections/testing~1int-reverse/key/0,
error: location /int accepts "number", "object" in schema test://example/int-string.schema, but locations used as keys may only be null-able integers, strings, or booleans,
error: location /int accepts "number", "object" in schema test://example/int-string.schema, but locations used as keys may only be null-able numbers, strings, or booleans,
},
Error {
scope: test://example/int-string#/collections/testing~1int-string/key/0,
error: location /int accepts "number", "object" in schema test://example/int-string.schema, but locations used as keys may only be null-able integers, strings, or booleans,
error: location /int accepts "number", "object" in schema test://example/int-string.schema, but locations used as keys may only be null-able numbers, strings, or booleans,
},
Error {
scope: test://example/int-string#/collections/testing~1int-string-ref-write-schema/key/0,
error: location /int accepts "number", "object" in schema test://example/int-string.schema, but locations used as keys may only be null-able integers, strings, or booleans,
error: location /int accepts "number", "object" in schema test://example/int-string.schema, but locations used as keys may only be null-able numbers, strings, or booleans,
},
Error {
scope: test://example/int-string#/collections/testing~1int-string-ref-write-schema/key/0,
error: location /int accepts "number", "object" in schema test://example/int-string#/collections/testing~1int-string-ref-write-schema/readSchema, but locations used as keys may only be null-able integers, strings, or booleans,
error: location /int accepts "number", "object" in schema test://example/int-string#/collections/testing~1int-string-ref-write-schema/readSchema, but locations used as keys may only be null-able numbers, strings, or booleans,
},
Error {
scope: test://example/int-string#/collections/testing~1int-string-rw/key/0,
error: location /int accepts "number", "object" in schema test://example/int-string.schema, but locations used as keys may only be null-able integers, strings, or booleans,
error: location /int accepts "number", "object" in schema test://example/int-string.schema, but locations used as keys may only be null-able numbers, strings, or booleans,
},
Error {
scope: test://example/int-string#/collections/testing~1int-string-rw/key/0,
error: location /int accepts "number", "object" in schema test://example/canonical/int-string-len.schema, but locations used as keys may only be null-able integers, strings, or booleans,
error: location /int accepts "number", "object" in schema test://example/canonical/int-string-len.schema, but locations used as keys may only be null-able numbers, strings, or booleans,
},
Error {
scope: test://example/int-string#/collections/testing~1int-string.v2/key/0,
error: location /int accepts "number", "object" in schema test://inlined/canonical/id, but locations used as keys may only be null-able integers, strings, or booleans,
error: location /int accepts "number", "object" in schema test://inlined/canonical/id, but locations used as keys may only be null-able numbers, strings, or booleans,
},
]

0 comments on commit 6622e0e

Please sign in to comment.