|
| 1 | +const JsonApiService = require("../../../lib/api/services/JsonApiService"); |
| 2 | +var JSONAPIValidator = require('jsonapi-validator').Validator; |
| 3 | +const expect = require('chai').expect; |
| 4 | + |
| 5 | +validateJSONapi = function(data) { |
| 6 | + var validator = new JSONAPIValidator(); |
| 7 | + |
| 8 | + try { |
| 9 | + validator.validate(data); |
| 10 | + return true; |
| 11 | + } catch (e) { |
| 12 | + return false; |
| 13 | + } |
| 14 | +}; |
| 15 | + |
| 16 | +sails = { |
| 17 | + config: {} |
| 18 | +}; |
| 19 | + |
| 20 | +describe("Is JsonApiService present", function() { |
| 21 | + |
| 22 | + it("Should be an object", function() { |
| 23 | + |
| 24 | + expect(JsonApiService).to.be.an('object'); |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +describe("Serialize Waterline validation error", function() { |
| 29 | + |
| 30 | + describe("Serialize default Waterline error object", function() { |
| 31 | + |
| 32 | + const waterlineErr = { |
| 33 | + "error": "E_VALIDATION", |
| 34 | + "status": 400, |
| 35 | + "summary": "1 attribute is invalid", |
| 36 | + "model": "User", |
| 37 | + "invalidAttributes": { |
| 38 | + "firstName": [ |
| 39 | + { |
| 40 | + "rule": "minLength", |
| 41 | + "message": "\"minLength\" validation rule failed for input: 'a'\nSpecifically, it threw an error. Details:\n undefined" |
| 42 | + } |
| 43 | + ] |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + const expectedOutput = { |
| 48 | + "errors": [ |
| 49 | + { |
| 50 | + "detail": "\"minLength\" validation rule failed for input: 'a'\nSpecifically, it threw an error. Details:\n undefined", |
| 51 | + "source": { |
| 52 | + "pointer": "data/attributes/firstName" |
| 53 | + } |
| 54 | + } |
| 55 | + ] |
| 56 | + }; |
| 57 | + |
| 58 | + var jsonApiError = { |
| 59 | + errors: JsonApiService.serializeWaterlineValidationError(waterlineErr) |
| 60 | + }; |
| 61 | + |
| 62 | + it("Should be valid JSON API", function() { |
| 63 | + |
| 64 | + expect(jsonApiError).to.satisfy(validateJSONapi); |
| 65 | + }); |
| 66 | + |
| 67 | + it("Should produce expected output", function() { |
| 68 | + |
| 69 | + expect(jsonApiError).to.deep.equal(expectedOutput); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("Serialize Waterline error object produces by sails-validation-hook", function() { |
| 74 | + |
| 75 | + const waterlineErr = { |
| 76 | + "error": "E_VALIDATION", |
| 77 | + "status": 400, |
| 78 | + "summary": "1 attribute is invalid", |
| 79 | + "model": "User", |
| 80 | + "invalidAttributes": { |
| 81 | + "firstName": [ |
| 82 | + { |
| 83 | + "rule": "minLength", |
| 84 | + "message": "\"minLength\" validation rule failed for input: 'a'\nSpecifically, it threw an error. Details:\n undefined" |
| 85 | + } |
| 86 | + ] |
| 87 | + }, |
| 88 | + "Errors": { |
| 89 | + "firstName": [ |
| 90 | + { |
| 91 | + "rule": "minLength", |
| 92 | + "message": "Minimum length is 2" |
| 93 | + } |
| 94 | + ] |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + const expectedOutput = { |
| 99 | + "errors": [ |
| 100 | + { |
| 101 | + "detail": "Minimum length is 2", |
| 102 | + "source": { |
| 103 | + "pointer": "data/attributes/firstName" |
| 104 | + } |
| 105 | + } |
| 106 | + ] |
| 107 | + }; |
| 108 | + |
| 109 | + var jsonApiError = { |
| 110 | + errors: JsonApiService.serializeWaterlineValidationError(waterlineErr) |
| 111 | + }; |
| 112 | + |
| 113 | + it("Should be valid JSON API", function() { |
| 114 | + |
| 115 | + expect(jsonApiError).to.satisfy(validateJSONapi); |
| 116 | + }); |
| 117 | + |
| 118 | + it("Should produce expected output", function() { |
| 119 | + |
| 120 | + expect(jsonApiError).to.deep.equal(expectedOutput); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments