Adapt authoring tool UI documentation

v1.0.0-rc.4

adapt-authoring-jsonschema/lib/Keywords.js

  1. import { App } from 'adapt-authoring-core'
  2. import bytes from 'bytes'
  3. import ms from 'ms'
  4. import path from 'path'
  5. /**
  6. * Adds some useful schema keywords
  7. * @memberof jsonschema
  8. * @extends {AbstractModule}
  9. */
  10. class Keywords {
  11. static get all () {
  12. const keywords = {
  13. isBytes: function () {
  14. return (value, { parentData, parentDataProperty }) => {
  15. try {
  16. parentData[parentDataProperty] = bytes.parse(value)
  17. return true
  18. } catch (e) {
  19. return false
  20. }
  21. }
  22. },
  23. isDate: function () {
  24. return (value, { parentData, parentDataProperty }) => {
  25. try {
  26. parentData[parentDataProperty] = new Date(value)
  27. return true
  28. } catch (e) {
  29. return false
  30. }
  31. }
  32. },
  33. isDirectory: function () {
  34. const doReplace = value => {
  35. const app = App.instance
  36. return [
  37. ['$ROOT', app.rootDir],
  38. ['$DATA', app.getConfig('dataDir')],
  39. ['$TEMP', app.getConfig('tempDir')]
  40. ].reduce((m, [k, v]) => {
  41. return m.startsWith(k) ? path.resolve(v, m.replace(k, '').slice(1)) : m
  42. }, value)
  43. }
  44. return (value, { parentData, parentDataProperty }) => {
  45. try {
  46. parentData[parentDataProperty] = doReplace(value)
  47. } catch (e) {}
  48. return true
  49. }
  50. },
  51. isTimeMs: function () {
  52. return (value, { parentData, parentDataProperty }) => {
  53. try {
  54. parentData[parentDataProperty] = ms(value)
  55. return true
  56. } catch (e) {
  57. return false
  58. }
  59. }
  60. }
  61. }
  62. return Object.entries(keywords).map(([keyword, compile]) => {
  63. return {
  64. keyword,
  65. type: 'string',
  66. modifying: true,
  67. schemaType: 'boolean',
  68. compile
  69. }
  70. })
  71. }
  72. }
  73. export default Keywords