How to use the tv4.defineKeyword function in tv4

To help you get started, we’ve selected a few tv4 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dcos / dcos-ui / src / js / utils / SchemaFormUtil.js View on Github external
// merged in the present definition we don't need this
      // empty group any more.
      delete defClone.properties[propName];
    }

    // Update property
    defClone.properties[propName] = prop;
  });

  return defClone;
}

/**
 * Introduce custom validation function to tv4
 */
tv4.defineKeyword("validator", (
  data,
  validationFunction //
) =>
  // Call the validation function and return:
  //
  //  - null     : No error
  //  - 'string' : The error description
  //
  validationFunction(data)
);

/**
 * The default tv4 error messages are quite bad.
 * Here are a bit more user-friendly alternatives
 *
 * The default language is 'en-gb', so by setting
github obniz / obniz / obniz / libs / wscommand / WSSchema.js View on Github external
const tv4 = require('tv4');

tv4.defineError('UNIQUE_KEYS', 10001, '{uniqueKeys} are must be unique value.');

tv4.defineKeyword('uniqueKeys', function(data, value, schema) {
  if (!Array.isArray(value)) {
    return null;
  }
  let targets = [];
  for (let key of value) {
    if (data[key] !== null && data[key] !== undefined) {
      targets.push(data[key]);
    }
  }
  let duplicated = targets.filter(function(x, i, self) {
    return self.indexOf(x) !== self.lastIndexOf(x);
  });
  if (duplicated.length > 0) {
    return {
      code: tv4.errorCodes.UNIQUE_KEYS,
      message: { uniqueKeys: value.join(',') },