akidee / schema.js

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

$ref #4

Closed dvv closed 14 years ago

dvv commented 14 years ago

Hi!

Suppose I have:

` model.A = {properties:{ id: String, title: String }}

model.B = {properties:{ id: String, a: ??? }} `

What should I substitute for definition of B.properties.a to make it a validating reference to an instance of model.A? Which value should I then feed to B.properties.a for schema to be valid?

TIA, --Vladimir

akidee commented 14 years ago

model.A = {properties:{ id: String, title: String },id:'bar'}

model.B = {properties:{ id: String, a: {$ref:'bar'} }}

Please read JSON Schema drafts ;)

dvv commented 14 years ago

Heh. That's fine, I tried it before, but consider:

` var model = {}; model.Language = { type: 'object', id: 'Language', properties: { id: {type: 'string'}, name: {type: 'string'} } };

var schema = Schema.create({ type: 'object', id: 'User', properties: { //lang: {$ref: model.Language}, lang: {$ref: 'Language'} } });

object = { lang: 'AAA' };

validation = schema.validate(object); console.log(object); `

After validation object.lang is undefined. So what values to put to reference properties?

TIA, --Vladimir

akidee commented 14 years ago

You need to call

Schema.resolveRefs();

after having define schemas with refs to internally resolve the refs. I hope this solves your problem?

dvv commented 14 years ago

Is there some IRC channel for quicker iterations? :) Schema.resolveRefs(); made things go further, but lang: {$ref: 'Language'} complains lang.id and lang.name are missing when I validate object.lang='a-dummy-string'. What to to put to object.lang for things to work? By sense a reference is a string such as /Language/en, say. No?

akidee commented 14 years ago

This is logical, because 'Language' wants to see an object, and not a string. What you want is:

lang: {$ref: 'Language.name'}

I remind you again: Read the both drafts I have used to implement schema.js very carefully. You can use #-based paths for relative and id-based paths for absolute referencing. A path is made up by dots.

dvv commented 14 years ago

I'm sorry, but to me schemas are magic, thus the questions. Nevertheless, if I do like say I get: TypeError: Cannot call method 'call' of undefined at Validation.type (/home/dvv/NODE/akidee/lib/node/schema/index.js:237:35)

akidee commented 14 years ago

Please send me the whole example.

dvv commented 14 years ago

Please, take a look at http://gist.github.com/612860

dvv commented 14 years ago

Ha. I should have used lang: {$ref: 'Language.properties.id'}!!! Damn, simple as the world :) Closed.

Thanks, --Vladimir