Skip to content

Commit

Permalink
Fixed partial validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
petersirka committed Aug 11, 2024
1 parent 43072b0 commit ec45a95
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 43 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
========================

- fixed inline declared middleware `&middleware` in the `ROUTE()` method
- fixed partial validation

========================
0.0.97
Expand Down
31 changes: 17 additions & 14 deletions jsonschema.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ function read_def(ref, definitions) {
}
}

function check_array(meta, error, value, stop, definitions, path) {
function check_array(meta, error, value, stop, definitions, path, partial) {

if (!(value instanceof Array)) {
if (meta.$$REQUIRED) {
Expand Down Expand Up @@ -336,7 +336,7 @@ function check_array(meta, error, value, stop, definitions, path) {
return;
}
case 'object':
tmp = check_object(type, error, val, null, stop, definitions);
tmp = check_object(type, error, val, null, stop, definitions, null, partial);
if (tmp != null) {
response.push(tmp);
break;
Expand All @@ -345,7 +345,7 @@ function check_array(meta, error, value, stop, definitions, path) {
return;
}
case 'array':
tmp = check_array(type, error, value, null, definitions);
tmp = check_array(type, error, value, null, definitions, null, partial);
if (tmp != null && (!meta.uniqueItems || response.indexOf(tmp) === -1)) {
response.push(tmp);
break;
Expand Down Expand Up @@ -382,7 +382,7 @@ function check_array(meta, error, value, stop, definitions, path) {
var ref = read_def(meta.items.$ref, definitions);
if (ref) {
var newerror = [];
tmp = transform(ref, newerror, val);
tmp = transform(ref, newerror, val, null, null, partial);
if (newerror.length) {
for (var err of newerror)
error.push(ref.$$ID + '.' + err, '@', path, i);
Expand Down Expand Up @@ -414,15 +414,15 @@ function check_array(meta, error, value, stop, definitions, path) {
break;
case 'object':
var newerror = new ErrorBuilder();
tmp = check_object(meta.items, newerror, val, stop, definitions, currentpath);
tmp = check_object(meta.items, newerror, val, stop, definitions, currentpath, partial);
if (newerror.length) {
for (var err of newerror.items)
error.push(meta.$$ID + '.' + err.name, err.error, currentpath, i);
} else if (tmp != null && (!meta.uniqueItems || response.indexOf(tmp) === -1))
response.push(tmp);
break;
case 'array':
tmp = check_array(meta.items, error, value, stop, definitions, currentpath);
tmp = check_array(meta.items, error, value, stop, definitions, currentpath, partial);
if (tmp != null && (!meta.uniqueItems || response.indexOf(tmp) === -1))
response.push(tmp);
break;
Expand Down Expand Up @@ -458,7 +458,7 @@ function check_array(meta, error, value, stop, definitions, path) {
return response;
}

function check_object(meta, error, value, response, stop, definitions, path) {
function check_object(meta, error, value, response, stop, definitions, path, partial) {

if (!value || typeof(value) !== 'object') {
if (meta.$$REQUIRED) {
Expand Down Expand Up @@ -499,6 +499,9 @@ function check_object(meta, error, value, response, stop, definitions, path) {
var currentpath = (path ? (path + '.') : '') + key;
var val = value[key];

if (partial && val === undefined)
continue;

switch (prop.type) {
case 'number':
case 'integer':
Expand Down Expand Up @@ -534,7 +537,7 @@ function check_object(meta, error, value, response, stop, definitions, path) {
break;
case 'object':
if (prop.properties) {
tmp = check_object(prop, error, val, null, null, definitions, currentpath);
tmp = check_object(prop, error, val, null, null, definitions, currentpath, partial);
if (tmp != null) {
response[key] = tmp;
count++;
Expand All @@ -546,7 +549,7 @@ function check_object(meta, error, value, response, stop, definitions, path) {
var ref = read_def(prop.$ref, definitions);
if (ref) {
var newerror = new ErrorBuilder();
tmp = transform(ref, newerror, val);
tmp = transform(ref, newerror, val, null, null, partial);
if (newerror.items.length) {
for (var err of newerror.items)
error.push(ref.$$ID + '.' + err, '@');
Expand All @@ -562,7 +565,7 @@ function check_object(meta, error, value, response, stop, definitions, path) {
}
break;
case 'array':
tmp = check_array(prop, error, val, null, definitions, currentpath);
tmp = check_array(prop, error, val, null, definitions, currentpath, partial);
if (tmp != null) {
response[key] = tmp;
count++;
Expand All @@ -572,7 +575,7 @@ function check_object(meta, error, value, response, stop, definitions, path) {
if (prop.$ref) {
var ref = F.jsonschemas[prop.$ref];
if (ref) {
tmp = check_object(ref, error, val, null, null, definitions, currentpath);
tmp = check_object(ref, error, val, null, null, definitions, currentpath, partial);
if (tmp != null) {
response[key] = tmp;
count++;
Expand Down Expand Up @@ -603,7 +606,7 @@ function check_object(meta, error, value, response, stop, definitions, path) {
return response;
}

function transform(meta, error, value, stop, path) {
function transform(meta, error, value, stop, path, partial) {

var output;

Expand All @@ -625,10 +628,10 @@ function transform(meta, error, value, stop, path) {
output = check_date(meta, error, value, null, path);
break;
case 'object':
output = check_object(meta, error, value, null, stop, meta, path);
output = check_object(meta, error, value, null, stop, meta, path, partial);
break;
case 'array':
output = check_array(meta, error, value, stop, meta, path);
output = check_array(meta, error, value, stop, meta, path, partial);
break;
default:
error.push('.type', undefined, path);
Expand Down
30 changes: 1 addition & 29 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7361,35 +7361,7 @@ exports.jsonschematransform = function(value, partial, error) {
if (!error)
error = new ErrorBuilder();

var response = null;

if (partial) {

var tmp = {};
var schema = {};

schema.properties = {};
schema.required = [];

for (let key in value) {
let prop = this.properties[key];
if (prop) {
tmp[key] = value[key];
schema.properties[key] = prop;
if (this.required && this.required.includes(key))
schema.required.push(key);
}
}

schema.$id = this.$id;
schema.$schema = this.$schema;
schema.type = this.type;

response = framework_jsonschema.transform(schema, error, tmp);

} else
response = framework_jsonschema.transform(this, error, value);

var response = framework_jsonschema.transform(this, error, value, null, null, partial);
return { error: error.is ? error : null, response: response };
};

Expand Down

0 comments on commit ec45a95

Please sign in to comment.