No Description
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.

parsing.js 3.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.assertKeyTypes = exports.requireKeys = void 0;
  4. const error_1 = require("../../../../error");
  5. function requireKeys(prefix, yaml, ...keys) {
  6. if (prefix) {
  7. prefix = prefix + ".";
  8. }
  9. for (const key of keys) {
  10. if (!yaml[key]) {
  11. throw new error_1.FirebaseError(`Expected key ${prefix + key.toString()}`);
  12. }
  13. }
  14. }
  15. exports.requireKeys = requireKeys;
  16. function assertKeyTypes(prefix, yaml, schema) {
  17. if (!yaml) {
  18. return;
  19. }
  20. for (const [keyAsString, value] of Object.entries(yaml)) {
  21. const key = keyAsString;
  22. const fullKey = prefix ? `${prefix}.${keyAsString}` : keyAsString;
  23. if (!schema[key] || schema[key] === "omit") {
  24. throw new error_1.FirebaseError(`Unexpected key ${fullKey}. You may need to install a newer version of the Firebase CLI.`);
  25. }
  26. let schemaType = schema[key];
  27. if (typeof schemaType === "function") {
  28. if (!schemaType(value)) {
  29. const friendlyName = value === null ? "null" : Array.isArray(value) ? "array" : typeof value;
  30. throw new error_1.FirebaseError(`${friendlyName} ${fullKey} failed validation`);
  31. }
  32. continue;
  33. }
  34. if (value === null) {
  35. if (schemaType.endsWith("?")) {
  36. continue;
  37. }
  38. throw new error_1.FirebaseError(`Expected ${fullKey} to be type ${schemaType}; was null`);
  39. }
  40. if (schemaType.endsWith("?")) {
  41. schemaType = schemaType.slice(0, schemaType.length - 1);
  42. }
  43. if (schemaType.includes("Field")) {
  44. const match = /^Field<(\w+)>$/.exec(schemaType);
  45. if (match && typeof value !== "string" && typeof value !== match[1]) {
  46. throw new error_1.FirebaseError(`Expected ${fullKey} to be Field<${match[1]}>; was ${typeof value}`);
  47. }
  48. continue;
  49. }
  50. if (schemaType === "List") {
  51. if (typeof value !== "string" && !Array.isArray(value)) {
  52. throw new error_1.FirebaseError(`Expected ${fullKey} to be a field list (array or list expression); was ${typeof value}`);
  53. }
  54. continue;
  55. }
  56. if (value === null) {
  57. if (schemaType.endsWith("?")) {
  58. continue;
  59. }
  60. throw new error_1.FirebaseError(`Expected ${fullKey}} to be type ${schemaType}; was null`);
  61. }
  62. if (schemaType.endsWith("?")) {
  63. schemaType = schemaType.slice(0, schemaType.length - 1);
  64. }
  65. if (schemaType === "string") {
  66. if (typeof value !== "string") {
  67. throw new error_1.FirebaseError(`Expected ${fullKey} to be type string; was ${typeof value}`);
  68. }
  69. }
  70. else if (schemaType === "number") {
  71. if (typeof value !== "number") {
  72. throw new error_1.FirebaseError(`Expected ${fullKey} to be type number; was ${typeof value}`);
  73. }
  74. }
  75. else if (schemaType === "boolean") {
  76. if (typeof value !== "boolean") {
  77. throw new error_1.FirebaseError(`Expected ${fullKey} to be type boolean; was ${typeof value}`);
  78. }
  79. }
  80. else if (schemaType === "array") {
  81. if (!Array.isArray(value)) {
  82. throw new error_1.FirebaseError(`Expected ${fullKey} to be type array; was ${typeof value}`);
  83. }
  84. }
  85. else if (schemaType === "object") {
  86. if (value === null || typeof value !== "object" || Array.isArray(value)) {
  87. throw new error_1.FirebaseError(`Expected ${fullKey} to be type object; was ${typeof value}`);
  88. }
  89. }
  90. else {
  91. throw new error_1.FirebaseError("YAML validation is missing a handled type " + schema[key]);
  92. }
  93. }
  94. }
  95. exports.assertKeyTypes = assertKeyTypes;