_validate()

in src/parsers/locale-messagesjson.js [84:161]


  _validate() {
    this.isValid = validateLocaleMessages(this.parsedJSON);
    if (!this.isValid) {
      log.debug('Schema Validation messages', validateLocaleMessages.errors);

      validateLocaleMessages.errors.forEach((error) => {
        const message = this.errorLookup(error);
        this.collector.addError(message);
      });
    }

    const regexp = new RegExp(MESSAGE_PLACEHOLDER_REGEXP, 'ig');
    const visitedLowercaseMessages = [];

    Object.keys(this.parsedJSON).forEach((message) => {
      if (!visitedLowercaseMessages.includes(message.toLowerCase())) {
        visitedLowercaseMessages.push(message.toLowerCase());
      } else {
        this.collector.addError({
          ...messages.JSON_DUPLICATE_KEY,
          file: this.filename,
          description: `Case-insensitive duplicate message name: ${message} found in JSON`,
          instancePath: `/${message}`,
        });
        this.isValid = false;
      }

      if (message.startsWith('@@')) {
        this.collector.addWarning({
          file: this.filename,
          instancePath: `/${message}`,
          ...messages.PREDEFINED_MESSAGE_NAME,
        });
      }

      const messageContent = this.parsedJSON[message].message;
      let matches = regexp.exec(messageContent);
      while (matches !== null) {
        if (!this.hasPlaceholder(message, matches[1])) {
          this.collector.addWarning({
            file: this.filename,
            instancePath: `/${message}/placeholders/${matches[1]}`,
            ...messages.MISSING_PLACEHOLDER,
          });
        }
        matches = regexp.exec(messageContent);
      }

      if (
        Object.prototype.hasOwnProperty.call(
          this.parsedJSON[message],
          'placeholders'
        )
      ) {
        const visitedLowercasePlaceholders = [];
        Object.keys(this.parsedJSON[message].placeholders).forEach(
          (placeholder) => {
            if (
              !visitedLowercasePlaceholders.includes(placeholder.toLowerCase())
            ) {
              visitedLowercasePlaceholders.push(placeholder.toLowerCase());
            } else {
              this.collector.addError({
                ...messages.JSON_DUPLICATE_KEY,
                file: this.filename,
                description: `Case-insensitive duplicate placeholder name: ${placeholder} found in JSON`,
                instancePath: `/${message}/placeholders/${placeholder}`,
              });
              this.isValid = false;
            }
          }
        );
      }

      // Reset the regexp
      regexp.lastIndex = 0;
    });
  }