Version 3.18.1
Show:

File: dataschema/js/dataschema-base.js

  1. /**
  2. * The DataSchema utility provides a common configurable interface for widgets to
  3. * apply a given schema to a variety of data.
  4. *
  5. * @module dataschema
  6. * @main dataschema
  7. */
  8. /**
  9. * Provides the base DataSchema implementation, which can be extended to
  10. * create DataSchemas for specific data formats, such XML, JSON, text and
  11. * arrays.
  12. *
  13. * @module dataschema
  14. * @submodule dataschema-base
  15. */
  16. var LANG = Y.Lang,
  17. /**
  18. * Base class for the YUI DataSchema Utility.
  19. * @class DataSchema.Base
  20. * @static
  21. */
  22. SchemaBase = {
  23. /**
  24. * Overridable method returns data as-is.
  25. *
  26. * @method apply
  27. * @param schema {Object} Schema to apply.
  28. * @param data {Object} Data.
  29. * @return {Object} Schema-parsed data.
  30. * @static
  31. */
  32. apply: function(schema, data) {
  33. return data;
  34. },
  35. /**
  36. * Applies field parser, if defined
  37. *
  38. * @method parse
  39. * @param value {Object} Original value.
  40. * @param field {Object} Field.
  41. * @return {Object} Type-converted value.
  42. */
  43. parse: function(value, field) {
  44. if(field.parser) {
  45. var parser = (LANG.isFunction(field.parser)) ?
  46. field.parser : Y.Parsers[field.parser+''];
  47. if(parser) {
  48. value = parser.call(this, value);
  49. }
  50. else {
  51. Y.log("Could not find parser for field " + Y.dump(field), "warn", "dataschema-json");
  52. }
  53. }
  54. return value;
  55. }
  56. };
  57. Y.namespace("DataSchema").Base = SchemaBase;
  58. Y.namespace("Parsers");