Skip to content

Commit ab78480

Browse files
committed
Split into two modules, add require.def support
1 parent e535419 commit ab78480

File tree

2 files changed

+59
-49
lines changed

2 files changed

+59
-49
lines changed

lib/links.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* JSON Schema link handler
3+
* Copyright (c) 2007 Kris Zyp SitePen (www.sitepen.com)
4+
* Licensed under the MIT (MIT-LICENSE.txt) license.
5+
*/
6+
require.def||(require.def=function(deps, factory){module.exports = factory();});
7+
require.def([], function(){
8+
var exports = {};
9+
exports.cacheLinks = true;
10+
exports.getLink = function(relation, instance, schema){
11+
// gets the URI of the link for the given relation based on the instance and schema
12+
// for example:
13+
// getLink(
14+
// "brother",
15+
// {"brother_id":33},
16+
// {links:[{rel:"brother", href:"Brother/{brother_id}"}]}) ->
17+
// "Brother/33"
18+
var links = schema.__linkTemplates;
19+
if(!links){
20+
links = {};
21+
var schemaLinks = schema.links;
22+
if(schemaLinks && schemaLinks instanceof Array){
23+
schemaLinks.forEach(function(link){
24+
/* // TODO: allow for multiple same-name relations
25+
if(links[link.rel]){
26+
if(!(links[link.rel] instanceof Array)){
27+
links[link.rel] = [links[link.rel]];
28+
}
29+
}*/
30+
links[link.rel] = link.href;
31+
});
32+
}
33+
if(exports.cacheLinks){
34+
schema.__linkTemplates = links;
35+
}
36+
}
37+
var linkTemplate = links[relation];
38+
return linkTemplate && exports.substitute(linkTemplate, instance);
39+
};
40+
41+
exports.substitute = function(linkTemplate, instance){
42+
return linkTemplate.replace(/\{([^\}]*)\}/g, function(t, property){
43+
var value = instance[decodeURIComponent(property)];
44+
if(value instanceof Array){
45+
// the value is an array, it should produce a URI like /Table/(4,5,8) and store.get() should handle that as an array of values
46+
return '(' + value.join(',') + ')';
47+
}
48+
return value;
49+
});
50+
};
51+
return exports;
52+
});

lib/json-schema.js renamed to lib/validate.js

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Copyright (c) 2007 Kris Zyp SitePen (www.sitepen.com)
66
* Licensed under the MIT (MIT-LICENSE.txt) license.
7-
To use the validator call JSONSchema.validate with an instance object and an optional schema object.
7+
To use the validator call the validate function with an instance object and an optional schema object.
88
If a schema is provided, it will be used to validate. If the instance object refers to a schema (self-validating),
99
that schema will be used to validate and the schema parameter is not necessary (if both exist,
1010
both validations will occur).
@@ -13,6 +13,8 @@ empty list will be returned. A validation error will have two properties:
1313
"property" which indicates which property had the error
1414
"message" which indicates what the error was
1515
*/
16+
require.def||(require.def=function(deps, factory){module.exports = factory();});
17+
require.def([], function(){
1618

1719
// setup primitive classes to be JSON Schema types
1820
String.type = "string";
@@ -22,8 +24,8 @@ exports.Integer = {type:"integer"};
2224
Object.type = "object";
2325
Array.type = "array";
2426
Date.type = "date";
25-
26-
exports.validate = function(/*Any*/instance,/*Object*/schema) {
27+
var exports = validate;
28+
exports.validate = function validate(/*Any*/instance,/*Object*/schema) {
2729
// Summary:
2830
// To use the validator call JSONSchema.validate with an instance object and an optional schema object.
2931
// If a schema is provided, it will be used to validate. If the instance object refers to a schema (self-validating),
@@ -237,50 +239,6 @@ exports.mustBeValid = function(result){
237239
throw new TypeError(result.errors.map(function(error){return "for property " + error.property + ': ' + error.message;}).join(", \n"));
238240
}
239241
}
240-
/* will add this later
241-
newFromSchema : function() {
242-
}
243-
*/
244242

245-
exports.cacheLinks = true;
246-
exports.getLink = function(relation, instance, schema){
247-
// gets the URI of the link for the given relation based on the instance and schema
248-
// for example:
249-
// getLink(
250-
// "brother",
251-
// {"brother_id":33},
252-
// {links:[{rel:"brother", href:"Brother/{brother_id}"}]}) ->
253-
// "Brother/33"
254-
var links = schema.__linkTemplates;
255-
if(!links){
256-
links = {};
257-
var schemaLinks = schema.links;
258-
if(schemaLinks && schemaLinks instanceof Array){
259-
schemaLinks.forEach(function(link){
260-
/* // TODO: allow for multiple same-name relations
261-
if(links[link.rel]){
262-
if(!(links[link.rel] instanceof Array)){
263-
links[link.rel] = [links[link.rel]];
264-
}
265-
}*/
266-
links[link.rel] = link.href;
267-
});
268-
}
269-
if(exports.cacheLinks){
270-
schema.__linkTemplates = links;
271-
}
272-
}
273-
var linkTemplate = links[relation];
274-
return linkTemplate && exports.substitute(linkTemplate, instance);
275-
};
276-
277-
exports.substitute = function(linkTemplate, instance){
278-
return linkTemplate.replace(/\{([^\}]*)\}/g, function(t, property){
279-
var value = instance[decodeURIComponent(property)];
280-
if(value instanceof Array){
281-
// the value is an array, it should produce a URI like /Table/(4,5,8) and store.get() should handle that as an array of values
282-
return '(' + value.join(',') + ')';
283-
}
284-
return value;
285-
});
286-
};
243+
return exports;
244+
});

0 commit comments

Comments
 (0)