Skip to content

Commit

Permalink
ValidationError#details is now safe to stringify. Improved example page.
Browse files Browse the repository at this point in the history
  • Loading branch information
garycourt committed Aug 24, 2011
1 parent 4ded5a2 commit f3fc4ad
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Fixed typo in draft-03 hyper-schema.json.
* Updated uri.js to latest version.
* Fixed bug with Report#addError. (Identified by cellog)
* ValidationError#details is now safe to stringify.
* Added example HTML page

## 3.5 (2011/03/07)
Expand Down
44 changes: 30 additions & 14 deletions example/index.html → examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@

/* Styles */

.container { padding-bottom: 20px; font-family: sans-serif; }
.row { margin-top: 20px; }
legend { font-weight: bold; font-size: 125%; }
label { display: block; font-weight: bold; }
.value_label { display: none; }
.value { height: 360px; }
.value { height: 360px; font-size: 12px; tab-size: 4; }
.value, .uri { width: 100%; }
.value, .uri, #report { font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif; }
#report { padding: 10px; white-space: pre-wrap; }
.value, .uri, .error code { font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif; }
.error { margin: 20px 0; font-size: 12px; }
.error:first-child { margin-top: 0; }
.error:last-child { margin-bottom: 0; }
.error_message { font-weight: bold; }
.error_schemaUri { margin-left: 15px; }
.error_attribute { margin-left: 15px; }
#report { padding: 20px; }
#report.valid { border: 1px solid #0f0; background-color: #efe; }
#report.invalid { border: 1px solid #f00; background-color: #fee; }

Expand Down Expand Up @@ -78,7 +85,7 @@ <h1>JSV: JSON Schema Validator</h1>
<fieldset id="json_properties">
<legend>Input</legend>
<label id="json_label" class="value_label" for="json">Value</label>
<textarea id="json" class="value" name="json"></textarea>
<textarea id="json" class="value" name="json" wrap="off"></textarea>
<label id="json_uri_label" class="uri_label" for="json_uri">URI</label>
<input id="json_uri" class="uri" type="text" name="json_uri" value="json">
</fieldset>
Expand All @@ -88,7 +95,7 @@ <h1>JSV: JSON Schema Validator</h1>
<fieldset id="schema_properties">
<legend>Schema</legend>
<label id="schema_label" class="value_label" for="schema">Value</label>
<textarea id="schema" class="value" name="schema"></textarea>
<textarea id="schema" class="value" name="schema" wrap="off"></textarea>
<label id="schema_uri_label" class="uri_label" for="schema_uri">URI</label>
<input id="schema_uri" class="uri" type="text" name="schema_uri" value="schema">
</fieldset>
Expand Down Expand Up @@ -214,14 +221,23 @@ <h1>JSV: JSON Schema Validator</h1>
reportElement.className = "invalid";
output = "";
for (var x = 0, xl = report.errors.length; x < xl; ++x) {
output += JSON.stringify(report.errors[x]) + "\n";
output += '<div class="error"><span class="error_uri">Problem with <code>' +
report.errors[x].uri +
'</code> : </span><span class="error_message">' +
report.errors[x].message +
'</span><br/><span class="error_schemaUri">Reported by <code>' +
report.errors[x].schemaUri +
'</code></span><br/><span class="error_attribute">Attribute "<code>' +
report.errors[x].attribute +
'</code>"</span><span class="error_details"> (<code>' +
JSON.stringify(report.errors[x].details) +
'</code>)</span></div>';
}
reportElement.innerHTML = output;
} else {
reportElement.className = "valid";
output = "Input is valid!";
reportElement.textContent = "Input is valid!";
}

reportElement.textContent = output;
} catch (e) {
var reportElement = document.getElementById("report");
reportElement.className = "invalid";
Expand All @@ -234,10 +250,10 @@ <h1>JSV: JSON Schema Validator</h1>
});

</script>
<script type="text/javascript" src="lib/uri/uri.js"></script>
<script type="text/javascript" src="lib/jsv.js"></script>
<script type="text/javascript" src="lib/json-schema-draft-03.js"></script>
<script type="text/javascript" src="lib/json-schema-draft-02.js"></script>
<script type="text/javascript" src="lib/json-schema-draft-01.js"></script>
<script type="text/javascript" src="../lib/uri/uri.js"></script>
<script type="text/javascript" src="../lib/jsv.js"></script>
<script type="text/javascript" src="../lib/json-schema-draft-03.js"></script>
<script type="text/javascript" src="../lib/json-schema-draft-02.js"></script>
<script type="text/javascript" src="../lib/json-schema-draft-01.js"></script>
</body>
</html>
39 changes: 30 additions & 9 deletions lib/jsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,30 @@ var exports = exports || this,
return uri;
}

function stripInstances(o) {
if (o instanceof JSONInstance) {
return o.getURI();
}

switch (typeOf(o)) {
case "undefined":
case "null":
case "boolean":
case "number":
case "string":
return o; //do nothing

case "object":
return mapObject(o, stripInstances);

case "array":
return mapArray(o, stripInstances);

default:
return o.toString();
}
}

/**
* The exception that is thrown when a schema fails to be created.
*
Expand Down Expand Up @@ -367,7 +391,7 @@ var exports = exports || this,
schemaUri : schema instanceof JSONInstance ? schema.getURI() : schema,
attribute : attr,
message : message,
details : details
details : stripInstances(details)
});
};

Expand Down Expand Up @@ -916,7 +940,7 @@ var exports = exports || this,
this._refs = {};
}
this._refs[name] = this.resolveURI(uri);
}
};

/**
* Returns the value of the provided reference name.
Expand All @@ -927,7 +951,7 @@ var exports = exports || this,

JSONSchema.prototype.getReference = function (name) {
return this._refs && this._refs[name];
}
};

/**
* Merges two schemas/instances together.
Expand Down Expand Up @@ -1000,7 +1024,6 @@ var exports = exports || this,
*/

Environment.prototype.createInstance = function (data, uri) {
var instance;
uri = formatURI(uri);

if (data instanceof JSONInstance && (!uri || data.getURI() === uri)) {
Expand All @@ -1021,8 +1044,6 @@ var exports = exports || this,
*/

Environment.prototype.createSchema = function (data, schema, uri) {
var instance,
initializer;
uri = formatURI(uri);

if (data instanceof JSONSchema && (!uri || data._uri === uri) && (!schema || data.getSchema().equals(schema))) {
Expand Down Expand Up @@ -1153,8 +1174,8 @@ var exports = exports || this,

schemaSchema = schema.getSchema();
report.schemaSchema = schemaSchema;
} catch (e) {
report.addError(e.uri, e.schemaUri, e.attribute, e.message, e.details);
} catch (f) {
report.addError(f.uri, f.schemaUri, f.attribute, f.message, f.details);
}

if (schemaSchema) {
Expand All @@ -1178,7 +1199,7 @@ var exports = exports || this,
[schemaURI, this._schemas[schemaURI]]
],
counter = 0,
item, uri, instance, schema, properties, key;
item, uri, instance, properties, key;

while (counter++ < stackSize && stack.length) {
item = stack.shift();
Expand Down

0 comments on commit f3fc4ad

Please sign in to comment.