Skip to content

Improve ValidationIssue enum #74

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

Merged
merged 6 commits into from
Jun 2, 2025
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
24 changes: 23 additions & 1 deletion Sources/JSONSchema/JSONValue/JSONValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public enum JSONValue: Hashable, Equatable, Sendable {
case boolean(Bool)
case null

public var primative: JSONType {
public var primitive: JSONType {
switch self {
case .string: return .string
case .number: return .number
Expand Down Expand Up @@ -109,3 +109,25 @@ extension JSONValue {
}
}
}

extension JSONValue: CustomStringConvertible {
public var description: String {
switch self {
case .string(let value):
return "\"\(value)\""
case .number(let value):
return String(value)
case .integer(let value):
return String(value)
case .object(let value):
let pairs = value.map { "\"\($0.key)\": \($0.value.description)" }
return "{\(pairs.joined(separator: ", "))}"
case .array(let value):
return "[\(value.map { $0.description }.joined(separator: ", "))]"
case .boolean(let value):
return value ? "true" : "false"
case .null:
return "null"
}
}
}
8 changes: 4 additions & 4 deletions Sources/JSONSchema/Keywords/Keywords+Applicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ extension Keywords {
if validIndices.isEmpty
&& !context.context.minContainsIsZero[self.context.location.dropLast(), default: false]
{
throw .containsInsufficientMatches
throw ValidationIssue.containsInsufficientMatches(count: instances.count, required: 1)
}

let annotationValue =
Expand Down Expand Up @@ -519,7 +519,7 @@ extension Keywords {
}

if validCount != 1 {
throw ValidationIssue.oneOfFailed
throw ValidationIssue.oneOfFailed(errors: [])
}
}
}
Expand Down Expand Up @@ -618,7 +618,7 @@ extension Keywords {
var subAnnotations = AnnotationContainer()
let result = subschema.validate(input, at: instanceLocation, annotations: &subAnnotations)
if !result.isValid {
throw .conditionalFailed
throw ValidationIssue.conditionalFailed(condition: "then", errors: result.errors ?? [])
}
annotations.merge(subAnnotations)
}
Expand Down Expand Up @@ -653,7 +653,7 @@ extension Keywords {
var subAnnotations = AnnotationContainer()
let result = subschema.validate(input, at: instanceLocation, annotations: &subAnnotations)
if !result.isValid {
throw .conditionalFailed
throw ValidationIssue.conditionalFailed(condition: "else", errors: result.errors ?? [])
}
annotations.merge(subAnnotations)
}
Expand Down
57 changes: 36 additions & 21 deletions Sources/JSONSchema/Keywords/Keywords+Assertion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ extension Keywords {
at location: JSONPointer,
using annotations: AnnotationContainer
) throws(ValidationIssue) {
let instanceType = input.primative
let instanceType = input.primitive
let isValid = allowedPrimitives.contains { allowedType in
allowedType.matches(instanceType: instanceType)
}
if !isValid {
throw .typeMismatch
throw ValidationIssue.typeMismatch(expected: allowedPrimitives, actual: instanceType)
}
}
}
Expand All @@ -72,7 +72,7 @@ extension Keywords {
using annotations: AnnotationContainer
) throws(ValidationIssue) {
if !enumCases.contains(input) {
throw .notEnumCase
throw ValidationIssue.notEnumCase(value: input, allowedValues: enumCases)
}
}
}
Expand All @@ -94,7 +94,7 @@ extension Keywords {
using annotations: AnnotationContainer
) throws(ValidationIssue) {
if input != value {
throw .constantMismatch
throw ValidationIssue.constantMismatch(expected: value, actual: input)
}
}
}
Expand Down Expand Up @@ -133,7 +133,7 @@ extension Keywords {
let remainder = double.remainder(dividingBy: divisor)
let tolerance = 1e-10 // A small tolerance value to account for floating-point precision
if abs(remainder) > tolerance {
throw ValidationIssue.notMultipleOf
throw ValidationIssue.notMultipleOf(number: double, multiple: divisor)
}
}
}
Expand All @@ -160,7 +160,7 @@ extension Keywords {
using annotations: AnnotationContainer
) throws(ValidationIssue) {
if let number = input.numeric, number > maxValue {
throw .exceedsMaximum
throw ValidationIssue.exceedsMaximum(number: number, maximum: maxValue)
}
}
}
Expand All @@ -185,7 +185,7 @@ extension Keywords {
using annotations: AnnotationContainer
) throws(ValidationIssue) {
if let number = input.numeric, number >= exclusiveMaxValue {
throw .exceedsExclusiveMaximum
throw ValidationIssue.exceedsExclusiveMaximum(number: number, maximum: exclusiveMaxValue)
}
}
}
Expand All @@ -211,7 +211,7 @@ extension Keywords {
using annotations: AnnotationContainer
) throws(ValidationIssue) {
if let number = input.numeric, number < minValue {
throw .belowMinimum
throw ValidationIssue.belowMinimum(number: number, minimum: minValue)
}
}
}
Expand All @@ -236,7 +236,7 @@ extension Keywords {
using annotations: AnnotationContainer
) throws(ValidationIssue) {
if let number = input.numeric, number <= exclusiveMinValue {
throw .belowExclusiveMinimum
throw ValidationIssue.belowExclusiveMinimum(number: number, minimum: exclusiveMinValue)
}
}
}
Expand Down Expand Up @@ -266,7 +266,7 @@ extension Keywords {
using annotations: AnnotationContainer
) throws(ValidationIssue) {
if let string = input.string, string.count > maxLength {
throw .exceedsMaxLength
throw ValidationIssue.exceedsMaxLength(string: string, maxLength: maxLength)
}
}
}
Expand All @@ -292,7 +292,7 @@ extension Keywords {
using annotations: AnnotationContainer
) throws(ValidationIssue) {
if let string = input.string, string.count < minLength {
throw .belowMinLength
throw ValidationIssue.belowMinLength(string: string, minLength: minLength)
}
}
}
Expand Down Expand Up @@ -325,7 +325,7 @@ extension Keywords {
) throws(ValidationIssue) {
if let string = input.string, let regex = regex {
if string.firstMatch(of: regex) == nil {
throw .patternMismatch
throw ValidationIssue.patternMismatch(string: string, pattern: value.string ?? "")
}
}
}
Expand Down Expand Up @@ -376,7 +376,7 @@ extension Keywords {
using annotations: AnnotationContainer
) throws(ValidationIssue) {
if let array = input.array, array.count > maxItems {
throw .exceedsMaxItems
throw ValidationIssue.exceedsMaxItems(count: array.count, maxItems: maxItems)
}
}
}
Expand All @@ -402,7 +402,7 @@ extension Keywords {
using annotations: AnnotationContainer
) throws(ValidationIssue) {
if let array = input.array, array.count < minItems {
throw .belowMinItems
throw ValidationIssue.belowMinItems(count: array.count, minItems: minItems)
}
}
}
Expand Down Expand Up @@ -430,7 +430,7 @@ extension Keywords {
if uniqueItemsRequired, let array = input.array {
let set = Set(array)
if set.count != array.count {
throw .itemsNotUnique
throw ValidationIssue.itemsNotUnique
}
}
}
Expand Down Expand Up @@ -465,11 +465,17 @@ extension Keywords {
switch containsAnnotation.value {
case .everyIndex:
if array.count > maxContains {
throw .containsExcessiveMatches
throw ValidationIssue.containsExcessiveMatches(
count: array.count,
maxAllowed: maxContains
)
}
case .indicies(let indicies):
if indicies.count > maxContains {
throw .containsExcessiveMatches
throw ValidationIssue.containsExcessiveMatches(
count: indicies.count,
maxAllowed: maxContains
)
}
}
}
Expand Down Expand Up @@ -508,11 +514,17 @@ extension Keywords {
switch containsAnnotation.value {
case .everyIndex:
if array.count < minContains {
throw .containsInsufficientMatches
throw ValidationIssue.containsInsufficientMatches(
count: array.count,
required: minContains
)
}
case .indicies(let indicies):
if indicies.count < minContains {
throw .containsInsufficientMatches
throw ValidationIssue.containsInsufficientMatches(
count: indicies.count,
required: minContains
)
}
}
}
Expand Down Expand Up @@ -545,7 +557,10 @@ extension Keywords {
guard let object = input.object else { return }

if object.count > maxProperties {
throw .exceedsMaxProperties
throw ValidationIssue.exceedsMaxProperties(
count: object.count,
maxProperties: maxProperties
)
}
}
}
Expand Down Expand Up @@ -573,7 +588,7 @@ extension Keywords {
guard let object = input.object else { return }

if object.count < minProperties {
throw ValidationIssue.belowMinProperties
throw ValidationIssue.belowMinProperties(count: object.count, minProperties: minProperties)
}
}
}
Expand Down
Loading
Loading