import PureYAML public extension PureYAML.Validation.Rule { /// 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 rule and /// ``numbersAreFinite`` complete the JSON-representability set. static var mappingKeysAreStrings: Self { Self(description: "Mapping keys are strings") { context in guard case let .mapping(mapping) = context.subject else { return [] } return mapping.pairs.compactMap { pair in guard pair.keyNode.stringValue == nil else { return nil } return PureYAML.Validation.Issue( severity: .error, reason: "Mapping key '\(pair.keyNode.description)' is not a string", path: context.path.appending(.complexKey(pair.keyNode)), ) } } when: { context in if case .mapping = context.subject { return true } return false } } /// 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: Self { Self(description: "Numbers are finite") { context in guard case let .double(number) = context.subject, !number.isFinite else { return [] } return [ PureYAML.Validation.Issue( severity: .error, reason: "Number '\(number)' is not finite", path: context.path, ), ] } when: { context in if case .double = context.subject { return true } return false } } }