-
-
Notifications
You must be signed in to change notification settings - Fork 4
fix type error handler to handle allOf conflicting type #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,25 +6,73 @@ import * as Instance from "@hyperjump/json-schema/instance/experimental"; | |||||||||||||||
| * @import { ErrorHandler, ErrorObject } from "../index.d.ts" | ||||||||||||||||
| */ | ||||||||||||||||
|
|
||||||||||||||||
| const ALL_TYPES = ["null", "boolean", "number", "string", "array", "object", "integer"]; | ||||||||||||||||
|
|
||||||||||||||||
| /** @type ErrorHandler */ | ||||||||||||||||
| const type = async (normalizedErrors, instance, localization) => { | ||||||||||||||||
| /** @type ErrorObject[] */ | ||||||||||||||||
| const errors = []; | ||||||||||||||||
|
|
||||||||||||||||
| if (normalizedErrors["https://json-schema.org/keyword/type"]) { | ||||||||||||||||
| let allowedTypes = new Set(ALL_TYPES); | ||||||||||||||||
| const failedTypeLocations = []; | ||||||||||||||||
|
|
||||||||||||||||
| for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/type"]) { | ||||||||||||||||
| if (!normalizedErrors["https://json-schema.org/keyword/type"][schemaLocation]) { | ||||||||||||||||
| const keyword = await getSchema(schemaLocation); | ||||||||||||||||
| const isValid = normalizedErrors["https://json-schema.org/keyword/type"][schemaLocation]; | ||||||||||||||||
| if (!isValid) { | ||||||||||||||||
| failedTypeLocations.push(schemaLocation); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| const keyword = await getSchema(schemaLocation); | ||||||||||||||||
| /** @type {string|string[]} */ | ||||||||||||||||
| const value = Schema.value(keyword); | ||||||||||||||||
| const types = Array.isArray(value) ? value : [value]; | ||||||||||||||||
| /** @type {Set<string>} */ | ||||||||||||||||
| const keywordTypes = new Set(types); | ||||||||||||||||
| allowedTypes = intersectTypeSets(allowedTypes, keywordTypes); | ||||||||||||||||
|
Comment on lines
+31
to
+32
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't need to write custom set intersection logic. Conceptually, if a schema allows numbers, it also allows integers. If you code for that relationship, the rest should all just work.
Suggested change
|
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if (allowedTypes.size === 0) { | ||||||||||||||||
| if (failedTypeLocations.length > 0) { | ||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's impossible for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are absolutely right here. One must fail. I will refine that . Regarding integer vs number, I wasn’t fully clear on how to treat them either. I saw the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you forget to remove this |
||||||||||||||||
| errors.push({ | ||||||||||||||||
| message: localization.getTypeErrorMessage(Schema.value(keyword), Instance.typeOf(instance)), | ||||||||||||||||
| message: localization.getConflictingTypeMessage(), | ||||||||||||||||
| instanceLocation: Instance.uri(instance), | ||||||||||||||||
| schemaLocation: schemaLocation | ||||||||||||||||
| schemaLocation: failedTypeLocations | ||||||||||||||||
| }); | ||||||||||||||||
| } | ||||||||||||||||
| } else if (failedTypeLocations.length > 0) { | ||||||||||||||||
| if (allowedTypes.has("number")) { | ||||||||||||||||
| allowedTypes.delete("integer"); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+44
to
+46
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't a great place to put this code. It would be more cohesive to put it after the for loop that builds |
||||||||||||||||
| errors.push({ | ||||||||||||||||
| message: localization.getTypeErrorMessage([...allowedTypes], Instance.typeOf(instance)), | ||||||||||||||||
| instanceLocation: Instance.uri(instance), | ||||||||||||||||
| schemaLocation: failedTypeLocations.length === 1 ? failedTypeLocations[0] : failedTypeLocations | ||||||||||||||||
| }); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return errors; | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * @param {Set<string>} a | ||||||||||||||||
| * @param {Set<string>} b | ||||||||||||||||
| * @returns {Set<string>} | ||||||||||||||||
| */ | ||||||||||||||||
| const intersectTypeSets = (a, b) => { | ||||||||||||||||
| /** @type {Set<string>} */ | ||||||||||||||||
| const intersection = new Set(); | ||||||||||||||||
| for (const type of a) { | ||||||||||||||||
| if (b.has(type)) { | ||||||||||||||||
| intersection.add(type); | ||||||||||||||||
| } else if (type === "integer" && b.has("number")) { | ||||||||||||||||
| intersection.add("integer"); | ||||||||||||||||
| } else if (type === "number" && b.has("integer")) { | ||||||||||||||||
| intersection.add("integer"); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| return intersection; | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| export default type; | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no reason to create a new set every time you initialize
allowedTypes.ALL_TYPEScan be a set.