default-values-definition-validator.spec.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/format';
  3. import {DataType} from './data-type.js';
  4. import {DefaultValuesDefinitionValidator} from './default-values-definition-validator.js';
  5. const S = new DefaultValuesDefinitionValidator();
  6. describe('DefaultValuesDefinitionValidator', function () {
  7. describe('validate', function () {
  8. it('requires a first argument to be a non-empty string', function () {
  9. const validate = v => () => S.validate(v, {});
  10. const error = v =>
  11. format(
  12. 'A first argument of DefaultValuesDefinitionValidator.validate ' +
  13. 'should be a non-empty String, but %s given.',
  14. v,
  15. );
  16. expect(validate('')).to.throw(error('""'));
  17. expect(validate(10)).to.throw(error('10'));
  18. expect(validate(true)).to.throw(error('true'));
  19. expect(validate(false)).to.throw(error('false'));
  20. expect(validate([])).to.throw(error('Array'));
  21. expect(validate({})).to.throw(error('Object'));
  22. expect(validate(undefined)).to.throw(error('undefined'));
  23. expect(validate(null)).to.throw(error('null'));
  24. validate('model')();
  25. });
  26. it('requires a second argument to be an object', function () {
  27. const validate = v => () => S.validate('model', v);
  28. const error = v =>
  29. format(
  30. 'The provided option "properties" of the model "model" ' +
  31. 'should be an Object, but %s given.',
  32. v,
  33. );
  34. expect(validate('str')).to.throw(error('"str"'));
  35. expect(validate(10)).to.throw(error('10'));
  36. expect(validate(true)).to.throw(error('true'));
  37. expect(validate(false)).to.throw(error('false'));
  38. expect(validate([])).to.throw(error('Array'));
  39. expect(validate(undefined)).to.throw(error('undefined'));
  40. expect(validate(null)).to.throw(error('null'));
  41. validate({})();
  42. });
  43. it('does not throw an error if no properties defined', function () {
  44. S.validate('model', {});
  45. });
  46. it('does not throw an error if no default value specified for a required property', function () {
  47. S.validate('model', {
  48. foo: {
  49. type: DataType.STRING,
  50. required: true,
  51. },
  52. });
  53. });
  54. it('does not throw an error if a default value matches a property type', function () {
  55. S.validate('model', {
  56. foo: {
  57. type: DataType.BOOLEAN,
  58. default: false,
  59. },
  60. });
  61. });
  62. it('does not throw an error if a default value from a factory function matches a property type', function () {
  63. S.validate('model', {
  64. foo: {
  65. type: DataType.BOOLEAN,
  66. default: () => false,
  67. },
  68. });
  69. });
  70. it('throws an error if a default value does not match a property type', function () {
  71. const throwable = () =>
  72. S.validate('model', {
  73. foo: {
  74. type: DataType.STRING,
  75. default: 10,
  76. },
  77. });
  78. expect(throwable).to.throw(
  79. 'A default value is invalid. The property "foo" of the model ' +
  80. '"model" must have a String, but Number given.',
  81. );
  82. });
  83. it('throws an error if a default value from a factory function does not match a property type', function () {
  84. const throwable = () =>
  85. S.validate('model', {
  86. foo: {
  87. type: DataType.STRING,
  88. default: () => 10,
  89. },
  90. });
  91. expect(throwable).to.throw(
  92. 'A default value is invalid. The property "foo" of the model ' +
  93. '"model" must have a String, but Number given.',
  94. );
  95. });
  96. it('throws an error if an array element of a default value does not match an item type', function () {
  97. const throwable = () =>
  98. S.validate('model', {
  99. foo: {
  100. type: DataType.ARRAY,
  101. itemType: DataType.STRING,
  102. default: [10],
  103. },
  104. });
  105. expect(throwable).to.throw(
  106. 'A default value is invalid. The array property "foo" of the model "model" ' +
  107. 'must have a String element, but Number given.',
  108. );
  109. });
  110. it('throws an error if an array element from a default value factory does not match an item type', function () {
  111. const throwable = () =>
  112. S.validate('model', {
  113. foo: {
  114. type: DataType.ARRAY,
  115. itemType: DataType.STRING,
  116. default: () => [10],
  117. },
  118. });
  119. expect(throwable).to.throw(
  120. 'A default value is invalid. The array property "foo" of the model "model" ' +
  121. 'must have a String element, but Number given.',
  122. );
  123. });
  124. });
  125. });