Skip to content
This repository
Browse code

Add more tests (validation of objects with the ‘singleError’ flag)

  • Loading branch information...
commit f5b405f0740c23b3708fd1ff7a42d4b2561a7347 1 parent 7ed3f89
František Hába authored

Showing 1 changed file with 374 additions and 0 deletions. Show diff stats Hide diff stats

  1. +374 0 tests/json/objectsSingleError.js
374 tests/json/objectsSingleError.js
... ... @@ -0,0 +1,374 @@
  1 +if (typeof module !== 'undefined' && module.exports) {
  2 + var expect = require('expect.js');
  3 + var amanda = require('../../dist/latest.js');
  4 +}
  5 +
  6 +/**
  7 + * Objects
  8 + * --------------------
  9 + */
  10 +suite('JSON/Objects (with the ‘singleError’ flag)', function() {
  11 +
  12 + /**
  13 + * Validator
  14 + */
  15 + var jsonSchemaValidator = amanda('json');
  16 +
  17 + /**
  18 + * Example #1
  19 + */
  20 + test('Test #1', function() {
  21 +
  22 + var count = 0;
  23 +
  24 + var schema = {
  25 + type: 'object',
  26 + properties: {
  27 + name: {
  28 + type: 'string'
  29 + }
  30 + }
  31 + };
  32 +
  33 + jsonSchemaValidator.validate({}, schema, function(error) {
  34 + count += 1;
  35 + expect(error).to.be(undefined);
  36 + });
  37 +
  38 + jsonSchemaValidator.validate({
  39 + name: 'František'
  40 + }, schema, function(error) {
  41 + count += 1;
  42 + expect(error).to.be(undefined);
  43 + });
  44 +
  45 + jsonSchemaValidator.validate({
  46 + name: 123
  47 + }, schema, function(error) {
  48 +
  49 + count += 1;
  50 +
  51 + expect(error).to.be.ok();
  52 + expect(error).to.have.property('0');
  53 + expect(error).to.have.property('length', 1);
  54 +
  55 + expect(error[0]).to.have.property('property', 'name');
  56 + expect(error[0]).to.have.property('propertyValue', 123);
  57 + expect(error[0]).to.have.property('attributeName', 'type');
  58 + expect(error[0]).to.have.property('attributeValue', 'string');
  59 +
  60 + });
  61 +
  62 + expect(count).to.be.eql(3);
  63 +
  64 + });
  65 +
  66 + /**
  67 + * Example #2
  68 + */
  69 + test('Test #2', function() {
  70 +
  71 + var count = 0;
  72 +
  73 + var schema = {
  74 + type: 'object',
  75 + properties: {
  76 + user: {
  77 + type: 'object',
  78 + properties: {
  79 + location: {
  80 + type: 'string'
  81 + },
  82 + language: {
  83 + type: 'string'
  84 + }
  85 + }
  86 + }
  87 + }
  88 + };
  89 +
  90 + jsonSchemaValidator.validate({}, schema, function(error) {
  91 + count += 1;
  92 + expect(error).to.be(undefined);
  93 + });
  94 +
  95 + jsonSchemaValidator.validate({
  96 + user: {}
  97 + }, schema, function(error) {
  98 + count += 1;
  99 + expect(error).to.be(undefined);
  100 + });
  101 +
  102 + jsonSchemaValidator.validate({
  103 + user: 'Hello'
  104 + }, schema, function(error) {
  105 +
  106 + count += 1;
  107 +
  108 + expect(error).to.be.ok();
  109 + expect(error).to.have.property('0');
  110 + expect(error).to.have.property('length', 1);
  111 +
  112 + expect(error[0]).to.have.property('property', 'user');
  113 + expect(error[0]).to.have.property('propertyValue', 'Hello');
  114 + expect(error[0]).to.have.property('attributeName', 'type');
  115 + expect(error[0]).to.have.property('attributeValue', 'object');
  116 +
  117 + });
  118 +
  119 + jsonSchemaValidator.validate({
  120 + user: {
  121 + location: 'Internet'
  122 + }
  123 + }, schema, function(error) {
  124 + count += 1;
  125 + expect(error).to.be(undefined);
  126 + });
  127 +
  128 + jsonSchemaValidator.validate({
  129 + user: {
  130 + language: 'en'
  131 + }
  132 + }, schema, function(error) {
  133 + count += 1;
  134 + expect(error).to.be(undefined);
  135 + });
  136 +
  137 + jsonSchemaValidator.validate({
  138 + user: {
  139 + location: 'Internet',
  140 + language: 'en'
  141 + }
  142 + }, schema, function(error) {
  143 + count += 1;
  144 + expect(error).to.be(undefined);
  145 + });
  146 +
  147 + jsonSchemaValidator.validate({
  148 + user: {
  149 + location: 123,
  150 + language: 'en'
  151 + }
  152 + }, schema, function(error) {
  153 +
  154 + count += 1;
  155 +
  156 + expect(error).to.be.ok();
  157 + expect(error).to.have.property('0');
  158 + expect(error).to.have.property('length', 1);
  159 +
  160 + expect(error[0]).to.have.property('property', 'user.location');
  161 + expect(error[0]).to.have.property('propertyValue', 123);
  162 + expect(error[0]).to.have.property('attributeName', 'type');
  163 + expect(error[0]).to.have.property('attributeValue', 'string');
  164 +
  165 + });
  166 +
  167 + jsonSchemaValidator.validate({
  168 + user: {
  169 + location: 'Internet',
  170 + language: 123
  171 + }
  172 + }, schema, function(error) {
  173 +
  174 + count += 1;
  175 +
  176 + expect(error).to.be.ok();
  177 + expect(error).to.have.property('0');
  178 + expect(error).to.have.property('length', 1);
  179 +
  180 + expect(error[0]).to.have.property('property', 'user.language');
  181 + expect(error[0]).to.have.property('propertyValue', 123);
  182 + expect(error[0]).to.have.property('attributeName', 'type');
  183 + expect(error[0]).to.have.property('attributeValue', 'string');
  184 +
  185 + });
  186 +
  187 + expect(count).to.be.eql(8);
  188 +
  189 + });
  190 +
  191 + /**
  192 + * Example #3
  193 + */
  194 + test('Test #3', function() {
  195 +
  196 + var count = 0;
  197 +
  198 + var schema = {
  199 + type: 'object',
  200 + properties: {
  201 + user: {
  202 + type: 'object',
  203 + properties: {
  204 + localization: {
  205 + type: 'object',
  206 + properties: {
  207 + location: {
  208 + type: 'string'
  209 + },
  210 + language: {
  211 + type: 'string'
  212 + }
  213 + }
  214 + }
  215 + }
  216 + }
  217 + }
  218 + };
  219 +
  220 + jsonSchemaValidator.validate({}, schema, function(error) {
  221 + count += 1;
  222 + expect(error).to.be(undefined);
  223 + });
  224 +
  225 + jsonSchemaValidator.validate({
  226 + user: {}
  227 + }, schema, function(error) {
  228 + count += 1;
  229 + expect(error).to.be(undefined);
  230 + });
  231 +
  232 + jsonSchemaValidator.validate({
  233 + user: {
  234 + localization: {}
  235 + }
  236 + }, schema, function(error) {
  237 + count += 1;
  238 + expect(error).to.be(undefined);
  239 + });
  240 +
  241 + jsonSchemaValidator.validate({
  242 + user: {
  243 + localization: {
  244 + location: 'Internet'
  245 + }
  246 + }
  247 + }, schema, function(error) {
  248 + count += 1;
  249 + expect(error).to.be(undefined);
  250 + });
  251 +
  252 + jsonSchemaValidator.validate({
  253 + user: {
  254 + localization: {
  255 + language: 'en'
  256 + }
  257 + }
  258 + }, schema, function(error) {
  259 + count += 1;
  260 + expect(error).to.be(undefined);
  261 + });
  262 +
  263 + jsonSchemaValidator.validate({
  264 + user: {
  265 + localization: {
  266 + location: 'Internet',
  267 + language: 'en'
  268 + }
  269 + }
  270 + }, schema, function(error) {
  271 + count += 1;
  272 + expect(error).to.be(undefined);
  273 + });
  274 +
  275 + jsonSchemaValidator.validate(123, schema, function(error) {
  276 +
  277 + count += 1;
  278 +
  279 + expect(error).to.be.ok();
  280 + expect(error).to.have.property('0');
  281 + expect(error).to.have.property('length', 1);
  282 +
  283 + expect(error[0]).to.have.property('property', '');
  284 + expect(error[0]).to.have.property('propertyValue', 123);
  285 + expect(error[0]).to.have.property('attributeName', 'type');
  286 + expect(error[0]).to.have.property('attributeValue', 'object');
  287 +
  288 + });
  289 +
  290 + jsonSchemaValidator.validate({
  291 + user: 123
  292 + }, schema, function(error) {
  293 +
  294 + count += 1;
  295 +
  296 + expect(error).to.be.ok();
  297 + expect(error).to.have.property('0');
  298 + expect(error).to.have.property('length', 1);
  299 +
  300 + expect(error[0]).to.have.property('property', 'user');
  301 + expect(error[0]).to.have.property('propertyValue', 123);
  302 + expect(error[0]).to.have.property('attributeName', 'type');
  303 + expect(error[0]).to.have.property('attributeValue', 'object');
  304 +
  305 + });
  306 +
  307 + jsonSchemaValidator.validate({
  308 + user: {
  309 + localization: 123
  310 + }
  311 + }, schema, function(error) {
  312 +
  313 + count += 1;
  314 +
  315 + expect(error).to.be.ok();
  316 + expect(error).to.have.property('0');
  317 + expect(error).to.have.property('length', 1);
  318 +
  319 + expect(error[0]).to.have.property('property', 'user.localization');
  320 + expect(error[0]).to.have.property('propertyValue', 123);
  321 + expect(error[0]).to.have.property('attributeName', 'type');
  322 + expect(error[0]).to.have.property('attributeValue', 'object');
  323 +
  324 + });
  325 +
  326 + jsonSchemaValidator.validate({
  327 + user: {
  328 + localization: {
  329 + location: 'Internet',
  330 + language: 123
  331 + }
  332 + }
  333 + }, schema, function(error) {
  334 +
  335 + count += 1;
  336 +
  337 + expect(error).to.be.ok();
  338 + expect(error).to.have.property('0');
  339 + expect(error).to.have.property('length', 1);
  340 +
  341 + expect(error[0]).to.have.property('property', 'user.localization.language');
  342 + expect(error[0]).to.have.property('propertyValue', 123);
  343 + expect(error[0]).to.have.property('attributeName', 'type');
  344 + expect(error[0]).to.have.property('attributeValue', 'string');
  345 +
  346 + });
  347 +
  348 + jsonSchemaValidator.validate({
  349 + user: {
  350 + localization: {
  351 + location: 123,
  352 + language: 'en'
  353 + }
  354 + }
  355 + }, schema, function(error) {
  356 +
  357 + count += 1;
  358 +
  359 + expect(error).to.be.ok();
  360 + expect(error).to.have.property('0');
  361 + expect(error).to.have.property('length', 1);
  362 +
  363 + expect(error[0]).to.have.property('property', 'user.localization.location');
  364 + expect(error[0]).to.have.property('propertyValue', 123);
  365 + expect(error[0]).to.have.property('attributeName', 'type');
  366 + expect(error[0]).to.have.property('attributeValue', 'string');
  367 +
  368 + });
  369 +
  370 + expect(count).to.be.eql(11);
  371 +
  372 + });
  373 +
  374 +});

0 comments on commit f5b405f

Please sign in to comment.
Something went wrong with that request. Please try again.