Permalink
Browse files

Add `Schema.create` function.

  • Loading branch information...
1 parent 449d7a5 commit 4536170d41eb76cbfd20d933dfb4cf8d2c84a69b @dervus dervus committed Feb 8, 2013
Showing with 42 additions and 1 deletion.
  1. +41 −0 lib/js-yaml/schema.js
  2. +1 −1 lib/js-yaml/schema/default.js
View
@@ -1,7 +1,9 @@
'use strict';
+var common = require('./common');
var YAMLException = require('./exception');
+var Type = require('./type');
function compileList(schema, name, result) {
@@ -59,4 +61,43 @@ function Schema(definition) {
}
+Schema.DEFAULT = null;
+
+
+Schema.create = function createSchema() {
+ var schemas, types;
+
+ switch (arguments.length) {
+ case 1:
+ schemas = Schema.DEFAULT;
+ types = arguments[0];
+ break;
+
+ case 2:
+ schemas = arguments[0];
+ types = arguments[1];
+ break;
+
+ default:
+ throw new YAMLException('Wrong number of arguments for Schema.create function');
+ }
+
+ schemas = common.toArray(schemas);
+ types = common.toArray(types);
+
+ if (!schemas.every(function (schema) { return schema instanceof Schema; })) {
+ throw new YAMLException('Specified list of super schemas (or a single Schema object) contains a non-Schema object.');
+ }
+
+ if (!types.every(function (type) { return type instanceof Type; })) {
+ throw new YAMLException('Specified list of YAML types (or a single Type object) contains a non-Type object.');
+ }
+
+ return new Schema({
+ include: schemas,
+ explicit: types
+ });
+};
+
+
module.exports = Schema;
@@ -4,7 +4,7 @@
var Schema = require('../schema');
-module.exports = new Schema({
+module.exports = Schema.DEFAULT = new Schema({
include: [
require('./safe')
],

0 comments on commit 4536170

Please sign in to comment.