mirror of
https://github.com/justinrainbow/json-schema.git
synced 2025-03-23 07:39:53 +01:00
remove webroot
This commit is contained in:
parent
64e08f484c
commit
9ab1249095
@ -1,17 +0,0 @@
|
||||
*{
|
||||
font-family: verdana, arial, sans-serif;
|
||||
}
|
||||
.campo{
|
||||
width:550px;
|
||||
height:230px;
|
||||
background-color:#eee;
|
||||
}
|
||||
.botao{
|
||||
width:300px;
|
||||
height:60px;
|
||||
}
|
||||
.comment{
|
||||
background-color:#eee;
|
||||
color:#363;
|
||||
font-size: .7em;
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
function print_r( array, return_val ) {
|
||||
// http://kevin.vanzonneveld.net
|
||||
// + original by: Michael White (http://getsprink.com)
|
||||
// + improved by: Ben Bryan
|
||||
// * example 1: print_r(1, true);
|
||||
// * returns 1: 1
|
||||
|
||||
var output = "", pad_char = " ", pad_val = 4;
|
||||
|
||||
var formatArray = function (obj, cur_depth, pad_val, pad_char) {
|
||||
if (cur_depth > 0) {
|
||||
cur_depth++;
|
||||
}
|
||||
|
||||
var base_pad = repeat_char(pad_val*cur_depth, pad_char);
|
||||
var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char);
|
||||
var str = "";
|
||||
|
||||
if (obj instanceof Array || obj instanceof Object) {
|
||||
str += "Array\n" + base_pad + "(\n";
|
||||
for (var key in obj) {
|
||||
if (obj[key] instanceof Array) {
|
||||
str += thick_pad + "["+key+"] => "+formatArray(obj[key], cur_depth+1, pad_val, pad_char);
|
||||
} else {
|
||||
str += thick_pad + "["+key+"] => " + obj[key] + "\n";
|
||||
}
|
||||
}
|
||||
str += base_pad + ")\n";
|
||||
} else if(obj == null || obj == undefined) {
|
||||
str = '';
|
||||
} else {
|
||||
str = obj.toString();
|
||||
}
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
var repeat_char = function (len, pad_char) {
|
||||
var str = "";
|
||||
for(var i=0; i < len; i++) {
|
||||
str += pad_char;
|
||||
};
|
||||
return str;
|
||||
};
|
||||
output = formatArray(array, 0, pad_val, pad_char);
|
||||
|
||||
if (return_val !== true) {
|
||||
document.write("<pre>" + output + "</pre>");
|
||||
return true;
|
||||
} else {
|
||||
return output;
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
$(document).ready(function(){
|
||||
$("#bt-validate-js").click(validateJs);
|
||||
$("#bt-validate-php").click(validatePhp);
|
||||
$("#bt-validate-php-type-cast-mode").click(validatePhpTypeCastMode);
|
||||
});
|
||||
|
||||
|
||||
function validatePhpTypeCastMode() {
|
||||
validatePhp(true);
|
||||
}
|
||||
|
||||
function validatePhp(typeCastMode) {
|
||||
if(typeCastMode == true) {
|
||||
typeCastMode = true;
|
||||
}
|
||||
else {
|
||||
typeCastMode = false;
|
||||
}
|
||||
|
||||
$('#resultados').html('. . . w o r k i n g . . . ');
|
||||
schema = $('#schema').val();
|
||||
json = $('#json').val();
|
||||
|
||||
$.getJSON(
|
||||
"validate.php",
|
||||
{"schema":schema,"json":json,"typeCastMode":typeCastMode},
|
||||
phpCallback
|
||||
);
|
||||
}
|
||||
|
||||
function phpCallback(json) {
|
||||
showResponse(json);
|
||||
}
|
||||
|
||||
function validateJs() {
|
||||
$('#resultados').html('. . . w o r k i n g . . . ');
|
||||
jsons = getJsons();
|
||||
//alert(print_r(jsons,true));
|
||||
if(jsons.schema) {
|
||||
validateResponse = JSONSchema.validate(jsons.json,jsons.schema);
|
||||
}
|
||||
else {
|
||||
validateResponse = JSONSchema.validate(jsons.json);
|
||||
}
|
||||
showResponse(validateResponse);
|
||||
}
|
||||
|
||||
function getJsons() {
|
||||
schema = $('#schema').val();
|
||||
json = $('#json').val();
|
||||
json = eval( '(' + json + ')' );
|
||||
if(schema) {
|
||||
schema = eval( '(' + schema + ')' );
|
||||
}
|
||||
return {"json":json,"schema":schema};
|
||||
}
|
||||
|
||||
function showResponse(validateResponse) {
|
||||
//alert(print_r(validateResponse,true));
|
||||
res = '<b>'+'JSON: '+(validateResponse.valid?' VALID':'INVALID')+'</B><BR/>';
|
||||
$.each(validateResponse.errors,function(i,item) {
|
||||
//alert(print_r(item,true));
|
||||
res += '<b>' + item.property + '</b> :: ' + item.message + '<br/><br/>';
|
||||
res += '<span class=comment>'+(i+":"+print_r(item,true))+"</span><BR/><br/>";
|
||||
});
|
||||
$('#resultados').html(res);
|
||||
}
|
11
webroot/js/jquery.js
vendored
11
webroot/js/jquery.js
vendored
File diff suppressed because one or more lines are too long
@ -1,213 +0,0 @@
|
||||
/**
|
||||
* JSONSchema Validator - Validates JavaScript objects using JSON Schemas
|
||||
* (http://www.json.com/json-schema-proposal/)
|
||||
*
|
||||
* Copyright (c) 2007 Kris Zyp SitePen (www.sitepen.com)
|
||||
* Licensed under the MIT (MIT-LICENSE.txt) licence.
|
||||
To use the validator call JSONSchema.validate with an instance object and an optional schema object.
|
||||
If a schema is provided, it will be used to validate. If the instance object refers to a schema (self-validating),
|
||||
that schema will be used to validate and the schema parameter is not necessary (if both exist,
|
||||
both validations will occur).
|
||||
The validate method will return an array of validation errors. If there are no errors, then an
|
||||
empty list will be returned. A validation error will have two properties:
|
||||
"property" which indicates which property had the error
|
||||
"message" which indicates what the error was
|
||||
*/
|
||||
|
||||
JSONSchema = {
|
||||
validate : function(/*Any*/instance,/*Object*/schema) {
|
||||
// Summary:
|
||||
// To use the validator call JSONSchema.validate with an instance object and an optional schema object.
|
||||
// If a schema is provided, it will be used to validate. If the instance object refers to a schema (self-validating),
|
||||
// that schema will be used to validate and the schema parameter is not necessary (if both exist,
|
||||
// both validations will occur).
|
||||
// The validate method will return an object with two properties:
|
||||
// valid: A boolean indicating if the instance is valid by the schema
|
||||
// errors: An array of validation errors. If there are no errors, then an
|
||||
// empty list will be returned. A validation error will have two properties:
|
||||
// property: which indicates which property had the error
|
||||
// message: which indicates what the error was
|
||||
//
|
||||
return this._validate(instance,schema,false);
|
||||
},
|
||||
checkPropertyChange : function(/*Any*/value,/*Object*/schema) {
|
||||
// Summary:
|
||||
// The checkPropertyChange method will check to see if an value can legally be in property with the given schema
|
||||
// This is slightly different than the validate method in that it will fail if the schema is readonly and it will
|
||||
// not check for self-validation, it is assumed that the passed in value is already internally valid.
|
||||
// The checkPropertyChange method will return the same object type as validate, see JSONSchema.validate for
|
||||
// information.
|
||||
//
|
||||
return this._validate(value,schema,true);
|
||||
},
|
||||
_validate : function(/*Any*/instance,/*Object*/schema,/*Boolean*/ _changing) {
|
||||
|
||||
|
||||
var errors2 = [];
|
||||
// validate a value against a property definition
|
||||
|
||||
function checkProp(value, schema, path,i) {
|
||||
if (typeof schema != 'object') {
|
||||
return;
|
||||
}
|
||||
path += path ? typeof i == 'number' ? '[' + i + ']' : typeof i == 'undefined' ? '' : '.' + i : i;
|
||||
function addError(message) {
|
||||
errors2.push({property:path,message:message});
|
||||
}
|
||||
if (_changing && schema.readonly)
|
||||
addError("is a readonly field, it can not be changed");
|
||||
/*
|
||||
if (schema instanceof Array) {
|
||||
if (!(value instanceof Array)) {
|
||||
return [{property:path,message:"An array tuple is required"}];
|
||||
}
|
||||
for (i =0; i < schema.length; i++) {
|
||||
errors2.concat(checkProp(value[i],schema[i],path,i));
|
||||
}
|
||||
return errors2;
|
||||
}
|
||||
*/
|
||||
if (schema['extends']) // if it extends another schema, it must pass that schema as well
|
||||
checkProp(value,schema['extends'],path,i);
|
||||
// validate a value against a type definition
|
||||
function checkType(type,value) {
|
||||
if (type) {
|
||||
if (typeof type == 'string' && type != 'any'
|
||||
&& (type == 'null' ? value !== null : typeof value != type)
|
||||
&& !(value instanceof Array && type == 'array')
|
||||
&& !(type == 'integer' && !(value%1)))
|
||||
return [{property:path,message:(typeof value) + " value found, but a " + type + " is required"}];
|
||||
if (type instanceof Array) {
|
||||
var unionErrors=[];
|
||||
for (var j = 0; j < type.length; j++) // a union type
|
||||
if (!(unionErrors=checkType(type[j],value)).length)
|
||||
break;
|
||||
if (unionErrors.length)
|
||||
return unionErrors;
|
||||
}
|
||||
else if (typeof type == 'object') {
|
||||
checkProp(value,type,path);
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
//if (value !== null) {
|
||||
if (value === undefined) {
|
||||
if (!schema.optional)
|
||||
addError("is missing and it is not optional");
|
||||
}
|
||||
else {
|
||||
errors2 = errors2.concat(checkType(schema.type,value));
|
||||
if (schema.disallow && !checkType(schema.disallow,value).length)
|
||||
addError(" disallowed value was matched");
|
||||
if (value instanceof Array) {
|
||||
if (schema.items) {
|
||||
if(schema.items instanceof Array) {
|
||||
for (var k = 0,l=value.length; k < l; k++) {
|
||||
if(k < schema.items.length) {
|
||||
errors2.concat(checkProp(value[k],schema.items[k],path,k));
|
||||
}
|
||||
else {
|
||||
if(schema.additionalProperties !== undefined) {
|
||||
if(schema.additionalProperties === false) {
|
||||
addError("The item " + i + "[" + k + "] is not defined in the objTypeDef and the objTypeDef does not allow additional properties");
|
||||
}
|
||||
else {
|
||||
errors2.concat(checkProp(value[k],schema.additionalProperties,path,k));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(value.length < schema.items.length) {
|
||||
for (var k = value.length; k < schema.items.length; k++) {
|
||||
errors2.concat(checkProp(undefined,schema.items[k],path,k));
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (var i =0,l=value.length; i < l; i++) {
|
||||
errors2.concat(checkProp(value[i],schema.items,path,i));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (schema.minItems && value.length < schema.minItems) {
|
||||
addError("There must be a minimum of " + schema.minItems + " in the array");
|
||||
}
|
||||
if (schema.maxItems && value.length > schema.maxItems) {
|
||||
addError("There must be a maximum of " + schema.maxItems + " in the array");
|
||||
}
|
||||
}
|
||||
else if (schema.properties && typeof value == 'object') {
|
||||
errors2.concat(checkObj(value,schema.properties,path,schema.additionalProperties));
|
||||
}
|
||||
if (schema.pattern && typeof value == 'string' && !value.match(schema.pattern))
|
||||
addError("does not match the regex pattern " + schema.pattern);
|
||||
if (schema.maxLength && typeof value == 'string' && (value.length > schema.maxLength))
|
||||
addError("may only be " + schema.maxLength + " characters long");
|
||||
if (schema.minLength && typeof value == 'string' && (value.length < schema.minLength))
|
||||
addError("must be at least " + schema.minLength + " characters long");
|
||||
if (typeof schema.minimum !== undefined && typeof value == typeof schema.minimum && schema.minimum > value)
|
||||
addError("must have a minimum value of " + schema.minimum);
|
||||
if (typeof schema.maximum !== undefined && typeof value == typeof schema.maximum && schema.maximum < value)
|
||||
addError("must have a maximum value of " + schema.maximum);
|
||||
if (schema['enum']) {
|
||||
var enumer = schema['enum'];
|
||||
var l = enumer.length;
|
||||
var found;
|
||||
for (var j = 0; j < l; j++)
|
||||
if (enumer[j]===value) {
|
||||
found=1;
|
||||
break;
|
||||
}
|
||||
if (!found) {
|
||||
addError("does not have a value in the enumeration " + enumer.join(", "));
|
||||
}
|
||||
}
|
||||
if (typeof schema.maxDecimal == 'number' && (value * Math.pow(10,schema.maxDecimal))%1) {
|
||||
addError("may only have " + schema.maxDecimal + " digits of decimal places");
|
||||
}
|
||||
}
|
||||
//}
|
||||
|
||||
}
|
||||
// validate an object against a schema
|
||||
function checkObj(instance,objTypeDef,path,additionalProp) {
|
||||
if (typeof objTypeDef =='object') {
|
||||
if (typeof instance != 'object' || instance instanceof Array)
|
||||
errors2.push({property:path,message:"an object is required"});
|
||||
|
||||
for (var i in objTypeDef)
|
||||
if (objTypeDef.hasOwnProperty(i)) {
|
||||
var value = instance[i];
|
||||
var propDef = objTypeDef[i];
|
||||
checkProp(value,propDef,path,i);
|
||||
}
|
||||
}
|
||||
for (var i in instance) {
|
||||
if (instance.hasOwnProperty(i) && objTypeDef && !objTypeDef[i] && additionalProp===false)
|
||||
errors2.push({property:path,message:(typeof value) + "The property " + i + " is not defined in the objTypeDef and the objTypeDef does not allow additional properties"});
|
||||
var requires = objTypeDef && objTypeDef[i] && objTypeDef[i].requires;
|
||||
if (requires && !(requires in instance))
|
||||
errors2.push({property:path,message:"the presence of the property " + i + " requires that " + requires + " also be present"});
|
||||
value = instance[i];
|
||||
if (objTypeDef && typeof objTypeDef == 'object' && !(i in objTypeDef))
|
||||
checkProp(value,additionalProp,path,i);
|
||||
// if (!_changing && value && value.type)
|
||||
// errors2 = errors2.concat(checkObj(value,value.type,path + '.' + i));
|
||||
if (!_changing && value && value.$schema)
|
||||
errors2 = errors2.concat(checkProp(value,value.$schema,path,i));
|
||||
}
|
||||
return errors2;
|
||||
}
|
||||
if (schema)
|
||||
checkProp(instance,schema,'','')
|
||||
if (!_changing && instance.$schema)
|
||||
checkProp(instance,instance.$schema,'','');
|
||||
return {valid:!errors2.length,errors:errors2};
|
||||
}
|
||||
/* will add this later
|
||||
newFromSchema : function() {
|
||||
}
|
||||
*/
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
<?php
|
||||
require_once('../../libs/JsonSchema.php');
|
||||
require_once('../../libs/JsonSchemaUndefined.php');
|
||||
Dbg::$quietMode = true;
|
||||
|
||||
if($_REQUEST['schema']) {
|
||||
$schema = json_decode($_REQUEST['schema']);
|
||||
if(!$schema) {
|
||||
trigger_error('Could not parse the SCHEMA object.',E_USER_ERROR);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$schema = null;
|
||||
}
|
||||
|
||||
$json = json_decode($_REQUEST['json']);
|
||||
if(!$json) {
|
||||
trigger_error('Could not parse the JSON object.',E_USER_ERROR);
|
||||
}
|
||||
|
||||
if($_REQUEST['typeCastMode'] == 'true') {
|
||||
JsonSchema::$checkMode = JsonSchema::CHECK_MODE_TYPE_CAST;
|
||||
}
|
||||
$result = JsonSchema::validate(
|
||||
$json,
|
||||
$schema
|
||||
);
|
||||
header('Content-type: application/x-json');
|
||||
echo json_encode($result);
|
||||
?>
|
@ -1,50 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<script type="text/javascript" src="../js/jsonschema.js"></script>
|
||||
<script type="text/javascript" src="../js/functions.js"></script>
|
||||
<script type="text/javascript" src="../js/jquery.js"></script>
|
||||
<script type="text/javascript" src="../js/interface.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="../css/interface.css" />
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<table>
|
||||
<tr>
|
||||
<td>
|
||||
<h3>JSON</h3>
|
||||
<textarea class='campo' id='json'>
|
||||
{
|
||||
"a":1,
|
||||
"b":"thing I know is that",
|
||||
"c":"I do exist"
|
||||
} </textarea>
|
||||
</td>
|
||||
<td rowspan="2" valign="top">
|
||||
<h3>Schema:</h3>
|
||||
<textarea class='campo' id='schema'>
|
||||
{
|
||||
"type":"object",
|
||||
"properties":{
|
||||
"a":{"type":"number"},
|
||||
"b":{"type":"string"}
|
||||
},
|
||||
"additionalProperties":false
|
||||
}
|
||||
</textarea>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<button id='bt-validate-js' class='botao'>
|
||||
Validate With Js
|
||||
</button>
|
||||
<button id='bt-validate-php' class='botao'>
|
||||
Validate With PHP
|
||||
</button>
|
||||
<button id='bt-validate-php-type-cast-mode' class='botao'>
|
||||
Validate With PHP (type cast mode)
|
||||
</button>
|
||||
<br/>
|
||||
<div id='resultados'>...</div>
|
||||
</body>
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user