在对象生命周期的不同阶段更改Meteor模式验证? [英] Changing Meteor schema validations at different stages of object lifecycle?

查看:91
本文介绍了在对象生命周期的不同阶段更改Meteor模式验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为购物车制作一个复杂的表格,我想在订单处理的不同阶段验证order对象的不同部分.

I'm making a complex form for a shopping cart and I want to validate different parts of my order object at different stages of the order process.

OrderSchema = new SimpleSchema({
  itemsOrdered: {
    type: [Object],
    optional: false,
  },
  totalPrice: {
    type: Number,
    optional: false,
  },
  status: {
    type: String,
    optional: false
  },
  termsAgreed: {
    type: Boolean,
    optional: false
  },
  customerAddress: {
    type: Object,
    optional: false
  },
  stripePaymentInfo: {
    type: Object,
    optional: true,
    blackbox: true
  },
});

IMO有点混乱,因为在order生命周期的不同阶段需要对不同的字段进行不同的验证.

It's kind of a mess IMO because the different fields need to be validated differently at different stages of the order's life cycle.

  • 仍在选择产品中间的用户还没有termsAgreedcustomerAddressstripePaymentInfo,但我不希望由于这两个原因而使验证失败,因为这还为时过早在订购过程中要对此进行验证

  • a user still in the middle of selecting products doesn't yet have termsAgreed, customerAddress, or stripePaymentInfo but I don't want validation to fail because of these two since it's still too early in the ordering process to be validating this

填写用户地址的用户不需要stripePaymentInfotermsAgreed.

a user filling out their customer address doesn't need stripePaymentInfo yet or termsAgreed.

我需要该架构在不同阶段成功验证,以触发诸如启用continue按钮之类的事情.

I need the schema to validate successfully at different stages to trigger things like enabling continue buttons.

问题是autoform总是想使用整个架构来验证整个对象,这对于像联系我"表单之类的简单对象以及没有太多生命周期的对象都是很好的选择.

The problem is that autoform always wants to use the entire schema to validate the entire object, which is fine for simple objects like Contact Me forms and such that don't have much of a lifecycle.

在生命周期的不同阶段,是否存在用于复杂对象验证的最佳实践或模式?

Is there a best practice or pattern for complex object validation at different stages of their life cycles?

推荐答案

我认为最干净的方法是拥有一个带有自定义验证的模式,该模式基于问题的事物的状态"进行验证,在本例中为.如果已经可以识别某个状态,则可以根据已经存在的状态来确定状态",也可以根据您放置在Order中的某个状态值"来确定该状态,例如枚举或其他内容.这样,您的验证就可以使用开箱即用的SimpleSchema功能,而且您不受任何可能破坏下游功能的一次性解决方案的束缚.

I think the cleanest would be to have one schema with custom validation that validates based on the "state" of the thing in questions, in this case the Order. The "state" could be determined on something that's pre-existing, if you can identify something there already, or by some "state value" you place in your Order, like an enumeration or something. This way your validation is using out-of-the-box SimpleSchema functionality, and you are not tied to any one-off solution that might break functionality downstream.

然后,您将编写一个自定义验证功能状态,或者在状态需要时进行验证,或者简单地返回验证是正确的方法,以使其正确通过而不会出现验证错误.

You would then write a custom validation function that would look at the state and either do the validation if the state calls for it, or simply returns that the validation is good to let it pass through correctly without validation errors.

这篇关于在对象生命周期的不同阶段更改Meteor模式验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆