Skip to content

Commit 2e76ccd

Browse files
committed
[bug] fix minDims validation
1 parent 86ca5fe commit 2e76ccd

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

jdict.m

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@
8080
% 'dependentSchemas', 'allOf', 'anyOf', 'oneOf', 'not',
8181
% 'if', 'then', 'else', 'title', 'description', 'examples',
8282
% '$comment', '$ref', '$defs', 'definitions'
83+
%
84+
% to enable validation of strongly-typed ND arrays, we also
85+
% extended JSON schema add added the following 3 keywords
86+
% 'binType': must be one of
87+
% 'uint8','int8','uint8','int8','uint8','int8','uint8','int8','single','double','logical'
88+
% 'minDims' and 'maxDims': sets the min/max dimension
89+
% vector (i.e. size(data)); when minDims/maxDims
90+
% contains a single integer, it expects data to be a
91+
% 1D vector of a valid length
8392
% schema = jd.attr2schema('title', 'Nested Example') exports the
8493
% schema-attributes as a JSON schema object
8594
%
@@ -165,8 +174,17 @@
165174
% 'age',struct('type','integer','minimum',0)),...
166175
% 'required',{{'name','age'}});
167176
% jd.setschema(schema);
168-
% err = jd.validate(); % validate data against schema
169-
% jd.getschema('json') % get schema as JSON string
177+
% err = jd.validate(); % validate data against schema
178+
% jd.getschema('json') % get schema as JSON string
179+
%
180+
% jd = jdict;
181+
% jd{':type'}='array'; % expects an array
182+
% jd{':binType'}='uint8'; % expects a uint8 array
183+
% jd{':minDims'}=2; % expects a 1D uint8 array of min length of 2
184+
% jd{':maxDims'}=6; % expects a 1D uint8 array of max length of 6
185+
% jd.setschema(jd.attr2schema()); % use ':keyword' attributes to create a schema
186+
% jd <= uint8([1,2,3]) % this works
187+
% %jd <= [1,2;3 4] % 2D array fails dims and binType check
170188
%
171189
% % loading complex data from REST-API
172190
% jd = jdict('https://neurojson.io:7777/cotilab/NeuroCaptain_2025');
@@ -1092,7 +1110,7 @@
10921110
allflags = [varargin(1:2:end); varargin(2:2:end)];
10931111
opt = struct(allflags{:});
10941112

1095-
schemaKeywords = {'type', 'enum', 'const', 'default', ...
1113+
schemaKeywords = {'type', 'enum', 'const', 'default', 'binType', 'minDims', 'maxDims', ...
10961114
'minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum', 'multipleOf', ...
10971115
'minLength', 'maxLength', 'pattern', 'format', ...
10981116
'items', 'minItems', 'maxItems', 'uniqueItems', 'contains', 'prefixItems', ...

jsonschema.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@
385385
end
386386
if (length(dims) == 1)
387387
if (~isvector(data))
388-
errors{end + 1} = sprintf('%s: length of dim is %d, violates %s %d', path, i, length(actualsize), dimtype{1}, length(dims));
388+
errors{end + 1} = sprintf('%s: length of dim is %d, violates %s length of %d', path, length(actualsize), dimtype{1}, length(dims));
389389
else
390390
actualsize = max(actualsize);
391391
end

0 commit comments

Comments
 (0)