16

I would like to declare objects schemas (that can instantiate variables with default values, having validation rules, etc.) like it can be done in mongoose, but on the client-side.

Haven't found any library offering it. Is there something I missed?

gwendall
  • 920
  • 15
  • 22
  • 1
    Just use Mongoose client-side http://mongoosejs.com/docs/unstable/docs/browser.html – Antonio Brandao Jul 22 '15 at 05:50
  • Too bad this is closed and @AntonioBrandao's comment is not so visible. It's the only objectively correct answer for how to use the Mongoose library on the client side. – Ian Paschal Jul 10 '18 at 13:45

4 Answers4

4

visit https://github.com/spumko/joi and I think you'll find a lot of joy[i].

The joi validation system is used to validate JavaScript objects based on a rich descriptive schema. Schema validation is the process of ensuring that objects match pre-defined expectations.

I mainly use it with nodejs but I think you'll find it nice also to use on the client side with http://browserify.org/.

Elad Amsalem
  • 1,490
  • 1
  • 12
  • 14
3

To solve this problem I created https://github.com/archangel-irk/storage

Mongoose-like schema validation, collections and documents on browser.

mmackh
  • 3,550
  • 3
  • 35
  • 51
1

Forms-angular: http://www.forms-angular.org/

You can describe schema at the client side level (part of schemas or nested schemas/ arrays of subdocuments ...) and forms with validators are automatically generated.

Moreover, you can just define mongoose schemas at the backend level and you end up with a great front-end/backend system for creating/loading/editing/deleting items in a mongo db.

Derek
  • 3,295
  • 3
  • 24
  • 31
0

edit: This library might be what you are looking for: https://github.com/molnarg/js-schema

What you are describing is essentially what classes do in languages such as java, c++, objective-c, etc. Unfortunately javascript doesn't really offer this functionality. The closest thing you can do is implementing Prototypes:

How does JavaScript .prototype work?

edit: on second thought, with Prototypes you should be able to achieve everything you asked for

Community
  • 1
  • 1
winkerVSbecks
  • 1,173
  • 1
  • 10
  • 24
  • He's not looking for a class implementation, but for (Mongoose-like) object modeling for client-side JS. – robertklep Oct 22 '13 at 20:19
  • Wouldn't prototype allow you to do object modelling? If not, could you explain why that is so? – winkerVSbecks Oct 22 '13 at 21:01
  • The reason he specifically mentions [Mongoose](http://mongoosejs.com/docs/guide.html) is that it offers object modeling using schemas, with typing, validations, defaults, etc. You can obviously implement all that yourself, but that's not what's being asked. – robertklep Oct 22 '13 at 21:07
  • That's fair. He is looking for a library to do this. Did a quick google search and found this: https://github.com/molnarg/js-schema – winkerVSbecks Oct 23 '13 at 02:30
  • Perhaps you can mention that in your answer :) – robertklep Oct 23 '13 at 06:00