Skip to content

Instantly share code, notes, and snippets.

@AdrianRossouw
Created November 19, 2010 16:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdrianRossouw/608c415da3de129ab83c to your computer and use it in GitHub Desktop.
Save AdrianRossouw/608c415da3de129ab83c to your computer and use it in GitHub Desktop.
require.paths.unshift(__dirname + '/modules', __dirname + '/lib/node', __dirname);
var JSV = require("jsv").JSV;
var _ = require('./modules/underscore/underscore')._;
var EntrySchema = {
id: 'EntrySchema',
properties : {
_id : { type : 'string' },
schema : { type : 'string' },
published : { type : 'boolean', 'default' : true, hidden: true },
},
}
var BasicEntrySchema = {
id: "BasicEntrySchema",
'extends' : EntrySchema,
description: 'A simple entry with a title and a body. Basic entries can be arranged in a hierarchical structure.',
properties : {
title : {
type: 'string',
title: 'Title',
render: 'title',
minLength : 1,
},
body : {
type: 'string',
title: 'Body',
render: 'markdown',
optional : true,
'default' : '',
},
'parent' : {
type: 'array',
title: 'Parents',
items: {
type: 'string'
},
optional: true
},
weight : {
type : 'integer',
title: 'Menu Weight',
'default': 0,
optional: true
},
},
};
var entry = {}
var env = JSV.createEnvironment();
schemaSchema = env.createSchema(BasicEntrySchema);
var defaults = {}
_.map( schemaSchema.getProperty('properties').getValue(), function (value, key, list) {
function type_map_defaults(type) {
switch (type) {
case 'object' : return {};
case 'array' : return [];
case 'string' : return '';
case 'integer' : return 0;
case 'number' : return 0;
case 'boolean' : return true;
default : return null;
}
}
if (value['default'] !== undefined) {
defaults[key] = value['default'];
}
else {
var optional = (value['optional'] !== undefined) ? value['optional'] : false;
if (!optional) {
defaults[key] = type_map_defaults(value['type'])
}
}
});
json = _.extend(defaults, { title : '' })
instance = env.createSchema(json, BasicEntrySchema);
var report = env.validate(json, EntrySchema);
var report = env.validate(json, BasicEntrySchema);
console.log(report.errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment