schema.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. define("dojox/json/schema", ["dojo/_base/kernel", "dojox", "dojo/_base/array"], function(dojo, dojox){
  2. dojo.getObject("json.schema", true, dojox);
  3. dojox.json.schema.validate = function(/*Any*/instance,/*Object*/schema){
  4. // summary:
  5. // To use the validator call this with an instance object and an optional schema object.
  6. // If a schema is provided, it will be used to validate. If the instance object refers to a schema (self-validating),
  7. // that schema will be used to validate and the schema parameter is not necessary (if both exist,
  8. // both validations will occur).
  9. // instance:
  10. // The instance value/object to validate
  11. // schema:
  12. // The schema to use to validate
  13. // description:
  14. // The validate method will return an object with two properties:
  15. // valid: A boolean indicating if the instance is valid by the schema
  16. // errors: An array of validation errors. If there are no errors, then an
  17. // empty list will be returned. A validation error will have two properties:
  18. // property: which indicates which property had the error
  19. // message: which indicates what the error was
  20. //
  21. return this._validate(instance,schema,false);
  22. };
  23. dojox.json.schema.checkPropertyChange = function(/*Any*/value,/*Object*/schema, /*String*/ property){
  24. // summary:
  25. // The checkPropertyChange method will check to see if an value can legally be in property with the given schema
  26. // This is slightly different than the validate method in that it will fail if the schema is readonly and it will
  27. // not check for self-validation, it is assumed that the passed in value is already internally valid.
  28. // The checkPropertyChange method will return the same object type as validate, see JSONSchema.validate for
  29. // information.
  30. // value:
  31. // The new instance value/object to check
  32. // schema:
  33. // The schema to use to validate
  34. // return:
  35. // see dojox.validate.jsonSchema.validate
  36. //
  37. return this._validate(value,schema, property || "property");
  38. };
  39. dojox.json.schema.mustBeValid = function(result){
  40. // summary:
  41. // This checks to ensure that the result is valid and will throw an appropriate error message if it is not
  42. // result: the result returned from checkPropertyChange or validate
  43. if(!result.valid){
  44. throw new TypeError(dojo.map(result.errors,function(error){return "for property " + error.property + ': ' + error.message;}).join(", "));
  45. }
  46. }
  47. dojox.json.schema._validate = function(/*Any*/instance,/*Object*/schema,/*Boolean*/ _changing){
  48. var errors = [];
  49. // validate a value against a property definition
  50. function checkProp(value, schema, path,i){
  51. var l;
  52. path += path ? typeof i == 'number' ? '[' + i + ']' : typeof i == 'undefined' ? '' : '.' + i : i;
  53. function addError(message){
  54. errors.push({property:path,message:message});
  55. }
  56. if((typeof schema != 'object' || schema instanceof Array) && (path || typeof schema != 'function')){
  57. if(typeof schema == 'function'){
  58. if(!(Object(value) instanceof schema)){
  59. addError("is not an instance of the class/constructor " + schema.name);
  60. }
  61. }else if(schema){
  62. addError("Invalid schema/property definition " + schema);
  63. }
  64. return null;
  65. }
  66. if(_changing && schema.readonly){
  67. addError("is a readonly field, it can not be changed");
  68. }
  69. if(schema['extends']){ // if it extends another schema, it must pass that schema as well
  70. checkProp(value,schema['extends'],path,i);
  71. }
  72. // validate a value against a type definition
  73. function checkType(type,value){
  74. if(type){
  75. if(typeof type == 'string' && type != 'any' &&
  76. (type == 'null' ? value !== null : typeof value != type) &&
  77. !(value instanceof Array && type == 'array') &&
  78. !(type == 'integer' && value%1===0)){
  79. return [{property:path,message:(typeof value) + " value found, but a " + type + " is required"}];
  80. }
  81. if(type instanceof Array){
  82. var unionErrors=[];
  83. for(var j = 0; j < type.length; j++){ // a union type
  84. if(!(unionErrors=checkType(type[j],value)).length){
  85. break;
  86. }
  87. }
  88. if(unionErrors.length){
  89. return unionErrors;
  90. }
  91. }else if(typeof type == 'object'){
  92. var priorErrors = errors;
  93. errors = [];
  94. checkProp(value,type,path);
  95. var theseErrors = errors;
  96. errors = priorErrors;
  97. return theseErrors;
  98. }
  99. }
  100. return [];
  101. }
  102. if(value === undefined){
  103. if(!schema.optional){
  104. addError("is missing and it is not optional");
  105. }
  106. }else{
  107. errors = errors.concat(checkType(schema.type,value));
  108. if(schema.disallow && !checkType(schema.disallow,value).length){
  109. addError(" disallowed value was matched");
  110. }
  111. if(value !== null){
  112. if(value instanceof Array){
  113. if(schema.items){
  114. if(schema.items instanceof Array){
  115. for(i=0,l=value.length; i<l; i++){
  116. errors.concat(checkProp(value[i],schema.items[i],path,i));
  117. }
  118. }else{
  119. for(i=0,l=value.length; i<l; i++){
  120. errors.concat(checkProp(value[i],schema.items,path,i));
  121. }
  122. }
  123. }
  124. if(schema.minItems && value.length < schema.minItems){
  125. addError("There must be a minimum of " + schema.minItems + " in the array");
  126. }
  127. if(schema.maxItems && value.length > schema.maxItems){
  128. addError("There must be a maximum of " + schema.maxItems + " in the array");
  129. }
  130. }else if(schema.properties){
  131. errors.concat(checkObj(value,schema.properties,path,schema.additionalProperties));
  132. }
  133. if(schema.pattern && typeof value == 'string' && !value.match(schema.pattern)){
  134. addError("does not match the regex pattern " + schema.pattern);
  135. }
  136. if(schema.maxLength && typeof value == 'string' && value.length > schema.maxLength){
  137. addError("may only be " + schema.maxLength + " characters long");
  138. }
  139. if(schema.minLength && typeof value == 'string' && value.length < schema.minLength){
  140. addError("must be at least " + schema.minLength + " characters long");
  141. }
  142. if(typeof schema.minimum !== undefined && typeof value == typeof schema.minimum &&
  143. schema.minimum > value){
  144. addError("must have a minimum value of " + schema.minimum);
  145. }
  146. if(typeof schema.maximum !== undefined && typeof value == typeof schema.maximum &&
  147. schema.maximum < value){
  148. addError("must have a maximum value of " + schema.maximum);
  149. }
  150. if(schema['enum']){
  151. var enumer = schema['enum'];
  152. l = enumer.length;
  153. var found;
  154. for(var j = 0; j < l; j++){
  155. if(enumer[j]===value){
  156. found=1;
  157. break;
  158. }
  159. }
  160. if(!found){
  161. addError("does not have a value in the enumeration " + enumer.join(", "));
  162. }
  163. }
  164. if(typeof schema.maxDecimal == 'number' &&
  165. (value.toString().match(new RegExp("\\.[0-9]{" + (schema.maxDecimal + 1) + ",}")))){
  166. addError("may only have " + schema.maxDecimal + " digits of decimal places");
  167. }
  168. }
  169. }
  170. return null;
  171. }
  172. // validate an object against a schema
  173. function checkObj(instance,objTypeDef,path,additionalProp){
  174. if(typeof objTypeDef =='object'){
  175. if(typeof instance != 'object' || instance instanceof Array){
  176. errors.push({property:path,message:"an object is required"});
  177. }
  178. for(var i in objTypeDef){
  179. if(objTypeDef.hasOwnProperty(i) && !(i.charAt(0) == '_' && i.charAt(1) == '_')){
  180. var value = instance[i];
  181. var propDef = objTypeDef[i];
  182. checkProp(value,propDef,path,i);
  183. }
  184. }
  185. }
  186. for(i in instance){
  187. if(instance.hasOwnProperty(i) && !(i.charAt(0) == '_' && i.charAt(1) == '_') && objTypeDef && !objTypeDef[i] && additionalProp===false){
  188. errors.push({property:path,message:(typeof value) + "The property " + i +
  189. " is not defined in the schema and the schema does not allow additional properties"});
  190. }
  191. var requires = objTypeDef && objTypeDef[i] && objTypeDef[i].requires;
  192. if(requires && !(requires in instance)){
  193. errors.push({property:path,message:"the presence of the property " + i + " requires that " + requires + " also be present"});
  194. }
  195. value = instance[i];
  196. if(objTypeDef && typeof objTypeDef == 'object' && !(i in objTypeDef)){
  197. checkProp(value,additionalProp,path,i);
  198. }
  199. if(!_changing && value && value.$schema){
  200. errors = errors.concat(checkProp(value,value.$schema,path,i));
  201. }
  202. }
  203. return errors;
  204. }
  205. if(schema){
  206. checkProp(instance,schema,'',_changing || '');
  207. }
  208. if(!_changing && instance && instance.$schema){
  209. checkProp(instance,instance.$schema,'','');
  210. }
  211. return {valid:!errors.length,errors:errors};
  212. };
  213. return dojox.json.schema;
  214. });