我想知道是否有任何更简单的方法来实现此规则。刚开始尝试使用Firestore。match /emails/{emailId} { allow write: if request.resource.data.attachments.size() == 0 || request.resource.data.attachments.size() == 1 && request.resource.data.attachments[0].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[0].filetype == 'image/png' || request.resource.data.attachments[0].filetype == 'image/jpg' || request.resource.data.attachments[0].filetype == 'application/vnd.ms-excel') || request.resource.data.attachments.size() == 2 && request.resource.data.attachments[0].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[0].filetype == 'image/png' || request.resource.data.attachments[0].filetype == 'image/jpg' || request.resource.data.attachments[0].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[1].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[1].filetype == 'image/png' || request.resource.data.attachments[1].filetype == 'image/jpg' || request.resource.data.attachments[1].filetype == 'application/vnd.ms-excel') || request.resource.data.attachments.size() == 3 && request.resource.data.attachments[0].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[0].filetype == 'image/png' || request.resource.data.attachments[0].filetype == 'image/jpg' || request.resource.data.attachments[0].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[1].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[1].filetype == 'image/png' || request.resource.data.attachments[1].filetype == 'image/jpg' || request.resource.data.attachments[1].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[2].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[2].filetype == 'image/png' || request.resource.data.attachments[2].filetype == 'image/jpg' || request.resource.data.attachments[2].filetype == 'application/vnd.ms-excel') || request.resource.data.attachments.size() == 4 && request.resource.data.attachments[0].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[0].filetype == 'image/png' || request.resource.data.attachments[0].filetype == 'image/jpg' || request.resource.data.attachments[0].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[1].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[1].filetype == 'image/png' || request.resource.data.attachments[1].filetype == 'image/jpg' || request.resource.data.attachments[1].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[2].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[2].filetype == 'image/png' || request.resource.data.attachments[2].filetype == 'image/jpg' || request.resource.data.attachments[2].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[3].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[3].filetype == 'image/png' || request.resource.data.attachments[3].filetype == 'image/jpg' || request.resource.data.attachments[3].filetype == 'application/vnd.ms-excel');} 最佳答案 我们不允许在规则中使用循环结构,因为我们依赖几种优化技术,这些技术对于更复杂的结构来说难度更大。我们也不会对规则执行的计算时间收费,这意味着我们不希望它们变得过于复杂和潜在的滥用。随着规则的重复,我强烈鼓励使用 function() 功能来简化。例如,这是 match /emails/{emailId} { function attachments (){ return request.resource.data.attachments(); } function attach_cnt () { return attachments().size(); } function valid_size(attach) { return attachments()[attach].fileSize < 3 * 1024 * 1024; } function valid_type(attach) { return (attachments()[attach].filetype == 'image/png' || attachments()[attach].filetype == 'image/jpg' || attachments()[attach].filetype == 'application/vnd.ms-excel'); } allow write: (attach_cnt() < 1 || (valid_size(0) && valid_type(0))) && (attach_cnt() < 2 || (valid_size(1) && valid_type(1))) && (attach_cnt() < 3 || (valid_size(2) && valid_type(2))) && (attach_cnt() < 4 || (valid_size(3) && valid_type(3)))}这是我如何简化它(值得仔细检查,因为我可能输入错误)。 我为正在访问的请求数据创建了一个函数 attachments,因为它是成束使用的——这使得浏览规则变得很容易。 我做了一个函数 attach_cnt 用于附件数量,因为它被检查了很多。 现在我看到每个附件都有一个文件大小限制,因此我为该测试创建了一个函数 valid_size,使用我可以传递的参数 attach。 接下来是函数 valid_type,它的工作方式相同,但进行了检查以确保它是一个有效的类型。 现在很明显,对于具有 2-4 个项目等的请求,对附件 0 执行相同的检查。重新排序某些逻辑使您只能检查每个附件一次。关于firebase - firestore 规则中可用的循环?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49225786/ 10-10 23:46