<commit_before>/**
 * Given a `Map` value and a GraphQL type, determine if the value will be
 * accepted for that type. This is primarily useful for validating the
 * runtime values of query variables.
 */
func isValidValue(value: Map, type: GraphQLInputType) throws -> [String] {
    // A value must be provided if the type is non-null.
    if let type = type as? GraphQLNonNull {
        if value == .null {
            if let namedType = type.ofType as? GraphQLNamedType {
                return ["Expected \"\(namedType.name)!\", found null."]
            }

            return ["Expected non-null value, found null."]
        }

        return try isValidValue(value: value, type: type.ofType as! GraphQLInputType)
    }

    guard value != .null else {
        return []
    }

    // Lists accept a non-list value as a list of one.
    if let type = type as? GraphQLList {
        let itemType = type.ofType

        if case .array(let values) = value {
            var errors: [String] = []

            for (index, item) in values.enumerated() {
                let e = try isValidValue(value: item, type: itemType as! GraphQLInputType).map {
                    "In element #\(index): \($0)"
                }
                errors.append(contentsOf: e)
            }

            return errors
        }

        return try isValidValue(value: value, type: itemType as! GraphQLInputType)
    }

    // Input objects check each defined field.
    if let type = type as? GraphQLInputObjectType {
        guard case .dictionary(let dictionary) = value else {
            return ["Expected \"\(type.name)\", found not an object."]
        }

        let fields = type.fields
        var errors: [String] = []

        // Ensure every provided field is defined.
        for (providedField, _) in dictionary {
            if fields[providedField] == nil {
                errors.append("In field \"\(providedField)\": Unknown field.")
            }
        }

        // Ensure every defined field is valid.
        for (fieldName, field) in fields {
            let newErrors = try isValidValue(value: value[fieldName], type: field.type).map {
                "In field \"\(fieldName)\": \($0)"
            }

            errors.append(contentsOf: newErrors)
        }

        return errors
    }

    guard let type = type as? GraphQLLeafType else {
        fatalError("Must be input type")
    }
    
    // Scalar/Enum input checks to ensure the type can parse the value to
    // a non-null value.
    let parseResult = try type.parseValue(value: value)
    
    if parseResult == .null {
        return ["Expected type \"\(type.name)\", found \(value)."]
    }
    
    return []
}<commit_msg>Improves error message for incorrect arg types<commit_after>/**
 * Given a `Map` value and a GraphQL type, determine if the value will be
 * accepted for that type. This is primarily useful for validating the
 * runtime values of query variables.
 */
func isValidValue(value: Map, type: GraphQLInputType) throws -> [String] {
    // A value must be provided if the type is non-null.
    if let type = type as? GraphQLNonNull {
        if value == .null {
            if let namedType = type.ofType as? GraphQLNamedType {
                return ["Expected \"\(namedType.name)!\", found null."]
            }

            return ["Expected non-null value, found null."]
        }

        return try isValidValue(value: value, type: type.ofType as! GraphQLInputType)
    }

    guard value != .null else {
        return []
    }

    // Lists accept a non-list value as a list of one.
    if let type = type as? GraphQLList {
        let itemType = type.ofType

        if case .array(let values) = value {
            var errors: [String] = []

            for (index, item) in values.enumerated() {
                let e = try isValidValue(value: item, type: itemType as! GraphQLInputType).map {
                    "In element #\(index): \($0)"
                }
                errors.append(contentsOf: e)
            }

            return errors
        }

        return try isValidValue(value: value, type: itemType as! GraphQLInputType)
    }

    // Input objects check each defined field.
    if let type = type as? GraphQLInputObjectType {
        guard case .dictionary(let dictionary) = value else {
            return ["Expected \"\(type.name)\", found not an object."]
        }

        let fields = type.fields
        var errors: [String] = []

        // Ensure every provided field is defined.
        for (providedField, _) in dictionary {
            if fields[providedField] == nil {
                errors.append("In field \"\(providedField)\": Unknown field.")
            }
        }

        // Ensure every defined field is valid.
        for (fieldName, field) in fields {
            let newErrors = try isValidValue(value: value[fieldName], type: field.type).map {
                "In field \"\(fieldName)\": \($0)"
            }

            errors.append(contentsOf: newErrors)
        }

        return errors
    }

    guard let type = type as? GraphQLLeafType else {
        fatalError("Must be input type")
    }
    
    // Scalar/Enum input checks to ensure the type can parse the value to
    // a non-null value.
    do {
        let parseResult = try type.parseValue(value: value)
        if parseResult == .null {
            return ["Expected type \"\(type.name)\", found \(value)."]
        }
    } catch {
        return ["Expected type \"\(type.name)\", found \(value)."]
    }
    
    return []
}
{'fraction_non_alphanumeric': 0.10716858789625361, 'fraction_numerical': 0.0007204610951008645, 'mean_word_length': 1.8331632653061225, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5990992', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1483, 'n_words': 534}