<commit_before>'use strict';


const { chain, pickBy, intersection, flatten } = require('lodash');
const { surround, toSentence } = require('underscore.string');

const { EngineError } = require('../../error');


/**
 * Validates `schema.readOnly`, which prevents an attribute from being updated.
 * It can still use default|transforms, which is why this is fired before the main input validation layer.
 **/
const validateReadOnly = async function ({ idl }) {
  // Gets a map of models' readonly attributes, e.g. { my_model: ['my_readonly_attribute', ...], ... }
  const readOnlyMap = chain(idl.models)
    .pickBy(modelIdl => modelIdl && modelIdl.properties)
    .mapValues(modelIdl => Object.keys(pickBy(modelIdl.properties, prop => prop.readOnly)))
    .value();

  return async function (input) {
    const { args: { data }, modelName } = input;

    // Check if any attribute in `data` is matching readonly attributes from IDL
    const dataKeys = data instanceof Array ? flatten(data.map(Object.keys)) : Object.keys(data);
    const readOnlyAttributes = readOnlyMap[modelName];
    const readOnlyDataKeys = intersection(dataKeys, readOnlyAttributes);

    // Report errors as a client-side exception
    if (readOnlyDataKeys.length > 0) {
      const keys = toSentence(readOnlyDataKeys.map(key => surround(key, '\'')));
      throw new EngineError(`Cannot update readonly attributes: ${keys}`, { reason: 'INPUT_VALIDATION' });
    }

    const response = await this.next(input);
    return response;
  };
};


module.exports = {
  validateReadOnly,
};<commit_msg>Fix actions crashing if no data specified<commit_after>'use strict';


const { chain, pickBy, intersection, flatten } = require('lodash');
const { surround, toSentence } = require('underscore.string');

const { EngineError } = require('../../error');


/**
 * Validates `schema.readOnly`, which prevents an attribute from being updated.
 * It can still use default|transforms, which is why this is fired before the main input validation layer.
 **/
const validateReadOnly = async function ({ idl }) {
  // Gets a map of models' readonly attributes, e.g. { my_model: ['my_readonly_attribute', ...], ... }
  const readOnlyMap = chain(idl.models)
    .pickBy(modelIdl => modelIdl && modelIdl.properties)
    .mapValues(modelIdl => Object.keys(pickBy(modelIdl.properties, prop => prop.readOnly)))
    .value();

  return async function (input) {
    const { args: { data }, modelName } = input;

    // Check if any attribute in `data` is matching readonly attributes from IDL
    if (data) {
      const dataKeys = data instanceof Array ? flatten(data.map(Object.keys)) : Object.keys(data);
      const readOnlyAttributes = readOnlyMap[modelName];
      const readOnlyDataKeys = intersection(dataKeys, readOnlyAttributes);

      // Report errors as a client-side exception
      if (readOnlyDataKeys.length > 0) {
        const keys = toSentence(readOnlyDataKeys.map(key => surround(key, '\'')));
        throw new EngineError(`Cannot update readonly attributes: ${keys}`, { reason: 'INPUT_VALIDATION' });
      }
    }

    const response = await this.next(input);
    return response;
  };
};


module.exports = {
  validateReadOnly,
};
{'fraction_non_alphanumeric': 0.13100436681222707, 'fraction_numerical': 0.0006238303181534623, 'mean_word_length': 3.808095952023988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2211380', 'n_tokens_mistral': 916, 'n_tokens_neox': 898, 'n_words': 272}