is-invalid-example.js 977 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var infer = require('./infer');
  6. /**
  7. * isInvalidExample()
  8. *
  9. * Check out the provided example and see if it fails inference via rttc.infer().
  10. *
  11. * Note:
  12. * Although `undefined` technically is inferred as "ref", this function
  13. * considers it an invalid example.
  14. *
  15. * ----------------------------------------------------------------------------
  16. * @param {JSON} example
  17. *
  18. * @return {===} truthy if the provided example is invalid,
  19. * false otherwise.
  20. */
  21. module.exports = function isInvalidExample(example, tolerateMultiItemArrays){
  22. if (_.isUndefined(example)) {
  23. return new Error('Invalid example: `undefined` is not a valid example.');
  24. }
  25. try {
  26. var typeSchema = infer(example);
  27. if (_.isUndefined(typeSchema) || _.isNull(typeSchema)) {
  28. return new Error('Invalid example: could not infer type schema.');
  29. }
  30. }
  31. catch (e) {
  32. return e;
  33. }
  34. return false;
  35. };