search

Home  >  Q&A  >  body text

node.js - 数据验证放到schema中还是还是controller中

拿注册接口来说。

之前写代码的时候是吧各种验证(为空,email格式验证等)放到controller里面,但是最近在读一些代码的时候,发现大多数都是使用mongoose的validator,直接在schema里面做验证。

我想问下,这两种方式哪个来说更好一些?

谢谢。

PHPzPHPz2783 days ago640

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 15:38:33

    Mongoose’s validator is derived from the validation feature of MongoDB 3.2+

    Please refer to the official documentation:

    https://docs.mongodb.com/mast...

    MongoDB provides functions similar to the Constraint of relational databases through validation.

    Here are some contexts for your reference, and you can choose according to the actual situation.

    In general, MongoDB has gradually introduced many new features, which on the one hand improve its functions and on the other hand give developers more help.

    For example, in the new version 3.4 obtained this month, MongoDB provides graph processing, which provides functions similar to Neo4J; MongoDB provides faceted navigation for navigation queries, which better supports dimensional queries; and also improves decimal Data model makes it more convenient to save price and other amount data.

    MongoDB has many new features that deserve developers’ attention. You can refer to the following new version’s feature update documents:

    New features in 3.4
    https://docs.mongodb.com/mast...

    New features in 3.2
    https://docs.mongodb.com/mast...

    Love MongoDB! Have Fun!

    reply
    0
  • Cancelreply