Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validator.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.assertType = exports.assertEnum = exports.assertHasOneOf = exports.assertHas = void 0;
  4. const clc = require("colorette");
  5. const error_1 = require("../error");
  6. function assertHas(obj, prop) {
  7. const objString = clc.cyan(JSON.stringify(obj));
  8. if (!obj[prop]) {
  9. throw new error_1.FirebaseError(`Must contain "${prop}": ${objString}`);
  10. }
  11. }
  12. exports.assertHas = assertHas;
  13. function assertHasOneOf(obj, props) {
  14. const objString = clc.cyan(JSON.stringify(obj));
  15. let count = 0;
  16. props.forEach((prop) => {
  17. if (obj[prop]) {
  18. count++;
  19. }
  20. });
  21. if (count !== 1) {
  22. throw new error_1.FirebaseError(`Must contain exactly one of "${props.join(",")}": ${objString}`);
  23. }
  24. }
  25. exports.assertHasOneOf = assertHasOneOf;
  26. function assertEnum(obj, prop, valid) {
  27. const objString = clc.cyan(JSON.stringify(obj));
  28. if (valid.indexOf(obj[prop]) < 0) {
  29. throw new error_1.FirebaseError(`Field "${prop}" must be one of ${valid.join(", ")}: ${objString}`);
  30. }
  31. }
  32. exports.assertEnum = assertEnum;
  33. function assertType(prop, propValue, type) {
  34. if (typeof propValue !== type) {
  35. throw new error_1.FirebaseError(`Property "${prop}" must be of type ${type}`);
  36. }
  37. }
  38. exports.assertType = assertType;