akidee / schema.js

Sophisticated JSON schema based data validation and adaptation
MIT License
173 stars 13 forks source link

Howto validate MongoDB instances? #2

Closed dvv closed 14 years ago

dvv commented 14 years ago

Hi!

MongoDB as primary keys uses _id properties. When non-strict validating such instances _id just gets deleted. How do I specify the default primary key name? Or should I specify it explicitly in every type: 'object' schema?

TIA, --Vladimir

akidee commented 14 years ago

You can define it explicitly with "type: 'object'", or you add "additionalProperties: true", so you can add other properties, including _id. It's your decision.

dvv commented 14 years ago

I see. Thought may be id is treated as kinda intrinsic property. Thanks --Vladimir

P.S. Would it be welcome to schema.js if I shared some drafts for fixtures based on json-schema?

--Vladimir

akidee commented 14 years ago

Fixtures - do you mean default schemas like address, geo point etc. ?

dvv commented 14 years ago

I mean a tool that can generate a bunch of instances filled with lorem ipsum data. I'll place the code to my fork of schema.js and ping you tomorrow (GMT+4 here)

akidee commented 14 years ago

Place this one into a separate repository, that depends on schema.js - API could be like this:

loremIpsumInstances = require('json-schema-fixture')(myJsonSchema, 100);

Good idea - I am interested, this is especially useful for automated testing.

dvv commented 14 years ago

yep. I made http://gist.github.com/605284 to show the idea. Frankly, I'd like you to refactor it into a bunch of pluggable methods, as you did in schema.js. That is why I thought it could be a plugin to schema.js schema walk routine, say, as a special kind of a fallback, which overrides default: .... That way we'd conforme the ideology of schema.js

What do you think?

akidee commented 14 years ago

1) This is your job ;) 2) Think of my model. There is Schema and Schema.Validation - what you want to do is Schema.Fixture - the properties you add to the schema for the fixture do not touch the validation, but it would be useful if you stick to a standard for fixture generation, if there is one. This has nothing to do with the walk routine, of course if would be great, if it is modular and implemented like Schema.Validation. To use Schema.Validation to generate fixtures is possible but misleading, because then you need a second schema for fixture generation. 3) I think of the next use for Schema: Schema.UI.

To conclude: Schema is for representing a Schema, Schema.x is for doing x with a schema.

dvv commented 14 years ago

Hardly, to say the truth... I planned to just add custom fallbacks and order to use them when validating. Particular parameters for generating particular datatypes could be specified as settings.

akidee commented 14 years ago

It is better, if you do not add it to schema.js ;) I have put a lot thoughts into the Schema.x way of doing it. As far as I understand you, it is very easy to transform my Validation implemantation to Schema.Fixture! Fallbacks are just there for validation, so if you use them for generating data, you cannot use 1 schema both for validation and fixture generation.