<commit_before>'use strict';


const { recurseMap } = require('../../utilities');
const { EngineStartupError } = require('../../error');


// Validate JSON schema `$data` properties
const validateData = function ({ idl }) {
  return recurseMap(idl, prop => {
    if (typeof prop !== 'object') { return prop; }

    // Find all $data properties
    for (const [key, value] of Object.entries(prop)) {
      if (!value.$data) { continue; }
      validateDataFormat({ value });
      // At the moment, main IDL validation does not support `$data`,
      // so we remove them
      delete prop[key];
    }

    return prop;
  }, false);
};

// Validates that $data is { $data: '...' }
const validateDataFormat = function ({ value }) {
  if (typeof value.$data !== 'string') {
    throw new EngineStartupError(`'$data' must be a string: ${JSON.stringify(value)}`, { reason: 'IDL_VALIDATION' });
  }
  if (Object.entries(value).length > 1) {
    throw new EngineStartupError(`'$data' must be the only property when specified: ${JSON.stringify(value)}`, {
      reason: 'IDL_VALIDATION',
    });
  }
};


module.exports = {
  validateData,
};
<commit_msg>Fix line width<commit_after>'use strict';


const { recurseMap } = require('../../utilities');
const { EngineStartupError } = require('../../error');


// Validate JSON schema `$data` properties
const validateData = function ({ idl }) {
  return recurseMap(idl, prop => {
    if (typeof prop !== 'object') { return prop; }

    // Find all $data properties
    for (const [key, value] of Object.entries(prop)) {
      if (!value.$data) { continue; }
      validateDataFormat({ value });
      // At the moment, main IDL validation does not support `$data`,
      // so we remove them
      delete prop[key];
    }

    return prop;
  }, false);
};

// Validates that $data is { $data: '...' }
const validateDataFormat = function ({ value }) {
  if (typeof value.$data !== 'string') {
    const message = `'$data' must be a string: ${JSON.stringify(value)}`;
    throw new EngineStartupError(message, { reason: 'IDL_VALIDATION' });
  }
  if (Object.entries(value).length > 1) {
    const val = JSON.stringify(value);
    const message = `'$data' must be the only property when specified: ${val}`;
    throw new EngineStartupError(message, { reason: 'IDL_VALIDATION' });
  }
};


module.exports = {
  validateData,
};

{'fraction_non_alphanumeric': 0.16631400761743545, 'fraction_numerical': 0.0008463817181548879, 'mean_word_length': 3.1989342806394316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6509411', 'n_tokens_mistral': 715, 'n_tokens_neox': 705, 'n_words': 213}