import PureYAML public extension PureYAML.Validation.BuiltinValidation { /// Reports any mapping key that is not a string. JSON object names must be /// strings, so a YAML complex key (a sequence or mapping used as a key) has no /// JSON form. PureYAML core already ships ``mappingKeysAreUnique``; this check and /// ``numbersAreFinite`` complete the JSON-representability set. static var mappingKeysAreStrings: PureYAML.Validation.Validation { PureYAML.Validation.Validation(description: "Mapping keys are strings") { context in context.subject.pairs.compactMap { pair in guard pair.keyNode.stringValue == nil else { return nil } return PureYAML.Validation.ValidationError( severity: .error, reason: "Mapping key '\(pair.keyNode.description)' is not a string", path: context.path.appending(.complexKey(pair.keyNode)), ) } } } /// Reports any non-finite number (NaN or infinity). JSON has no literal for /// these, so a value tree carrying one is not representable as JSON. static var numbersAreFinite: PureYAML.Validation.Validation { PureYAML.Validation.Validation(description: "Numbers are finite") { context in guard case let .double(number) = context.subject, !number.isFinite else { return [] } return [ PureYAML.Validation.ValidationError( severity: .error, reason: "Number '\(number)' is not finite", path: context.path, ), ] } when: { context in if case .double = context.subject { return true } return false } } }