def validate(data_dict, required, optional, filter_unknown_fields=False):
    """Make sure all required fields are present and all other fields are
    optional. If an unknown field is found and filter_unknown_fields is False,
    return an error. Otherwise, just filter the field out."""
    final = {}
    for key in required:
        if not data_dict.get(key):
            return "missing required key: " + key
    for key in data_dict:
        if key in required or key in optional:
            final[key] = data_dict[key]
        elif not filter_unknown_fields:
            return "unknown key: " + key

    return final