property-validator-registry.spec.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {regexpValidator} from './builtin/index.js';
  4. import {maxLengthValidator} from './builtin/index.js';
  5. import {minLengthValidator} from './builtin/index.js';
  6. import {PropertyValidatorRegistry} from './property-validator-registry.js';
  7. describe('PropertyValidatorRegistry', function () {
  8. it('has builtin validators', function () {
  9. const pvr = new PropertyValidatorRegistry();
  10. expect(pvr['_validators']).to.be.eql({
  11. maxLength: maxLengthValidator,
  12. minLength: minLengthValidator,
  13. regexp: regexpValidator,
  14. });
  15. });
  16. describe('addValidator', function () {
  17. it('adds a given validator with the name', function () {
  18. const pvr = new PropertyValidatorRegistry();
  19. const myValidator = () => undefined;
  20. const res = pvr.addValidator('myValidator', myValidator);
  21. expect(res).to.be.eq(pvr);
  22. expect(pvr['_validators']['myValidator']).to.be.eq(myValidator);
  23. });
  24. it('requires the given name to be a non-empty string', function () {
  25. const pvr = new PropertyValidatorRegistry();
  26. const throwable = v => () => pvr.addValidator(v, () => undefined);
  27. const error = v =>
  28. format(
  29. 'A name of the property validator must ' +
  30. 'be a non-empty String, but %s was given.',
  31. v,
  32. );
  33. expect(throwable('')).to.throw(error('""'));
  34. expect(throwable(10)).to.throw(error('10'));
  35. expect(throwable(0)).to.throw(error('0'));
  36. expect(throwable(false)).to.throw(error('false'));
  37. expect(throwable(undefined)).to.throw(error('undefined'));
  38. expect(throwable(null)).to.throw(error('null'));
  39. expect(throwable({})).to.throw(error('Object'));
  40. expect(throwable([])).to.throw(error('Array'));
  41. expect(throwable(() => undefined)).to.throw(error('Function'));
  42. throwable('str')();
  43. });
  44. it('throws an error if the given name already exists', function () {
  45. const pvr = new PropertyValidatorRegistry();
  46. pvr.addValidator('test', () => undefined);
  47. const throwable = () => pvr.addValidator('test', () => undefined);
  48. expect(throwable).to.throw(
  49. 'The property validator "test" is already defined.',
  50. );
  51. });
  52. it('requires the given validator to be a function', function () {
  53. const pvr = new PropertyValidatorRegistry();
  54. const throwable = v => () => pvr.addValidator('test', v);
  55. const error = v =>
  56. format(
  57. 'The property validator "test" must be a Function, but %s was given.',
  58. v,
  59. );
  60. expect(throwable('str')).to.throw(error('"str"'));
  61. expect(throwable('')).to.throw(error('""'));
  62. expect(throwable(10)).to.throw(error('10'));
  63. expect(throwable(0)).to.throw(error('0'));
  64. expect(throwable(false)).to.throw(error('false'));
  65. expect(throwable(undefined)).to.throw(error('undefined'));
  66. expect(throwable(null)).to.throw(error('null'));
  67. expect(throwable({})).to.throw(error('Object'));
  68. expect(throwable([])).to.throw(error('Array'));
  69. throwable(() => undefined)();
  70. });
  71. });
  72. describe('hasValidator', function () {
  73. it('returns false for a not existing name', function () {
  74. const pvr = new PropertyValidatorRegistry();
  75. expect(pvr.hasValidator('str')).to.be.false;
  76. expect(pvr.hasValidator('')).to.be.false;
  77. expect(pvr.hasValidator(10)).to.be.false;
  78. expect(pvr.hasValidator(0)).to.be.false;
  79. expect(pvr.hasValidator(true)).to.be.false;
  80. expect(pvr.hasValidator(false)).to.be.false;
  81. expect(pvr.hasValidator(null)).to.be.false;
  82. expect(pvr.hasValidator(undefined)).to.be.false;
  83. expect(pvr.hasValidator({})).to.be.false;
  84. expect(pvr.hasValidator([])).to.be.false;
  85. expect(pvr.hasValidator(() => undefined)).to.be.false;
  86. });
  87. it('returns true for an existing name', function () {
  88. const pvr = new PropertyValidatorRegistry();
  89. expect(pvr.hasValidator('test')).to.be.false;
  90. pvr.addValidator('test', () => undefined);
  91. expect(pvr.hasValidator('test')).to.be.true;
  92. });
  93. });
  94. describe('getValidator', function () {
  95. it('returns validator by its name', function () {
  96. const pvr = new PropertyValidatorRegistry();
  97. const myValidator1 = () => undefined;
  98. const myValidator2 = () => undefined;
  99. pvr.addValidator('foo', myValidator1);
  100. pvr.addValidator('bar', myValidator2);
  101. const res1 = pvr.getValidator('foo');
  102. const res2 = pvr.getValidator('bar');
  103. expect(res1).to.be.eq(myValidator1);
  104. expect(res2).to.be.eq(myValidator2);
  105. });
  106. it('throws an error for a not existed name', function () {
  107. const pvr = new PropertyValidatorRegistry();
  108. const throwable = v => () => pvr.getValidator(v);
  109. const error = v => format('The property validator %s is not defined.', v);
  110. expect(throwable('str')).to.throw(error('"str"'));
  111. expect(throwable('')).to.throw(error('""'));
  112. expect(throwable(10)).to.throw(error('10'));
  113. expect(throwable(0)).to.throw(error('0'));
  114. expect(throwable(true)).to.throw(error('true'));
  115. expect(throwable(false)).to.throw(error('false'));
  116. expect(throwable(null)).to.throw(error('null'));
  117. expect(throwable(undefined)).to.throw(error('undefined'));
  118. expect(throwable({})).to.throw(error('Object'));
  119. expect(throwable([])).to.throw(error('Array'));
  120. expect(throwable(() => undefined)).to.throw(error('Function'));
  121. });
  122. });
  123. });