validate_shapes.mjs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env node
  2. // validate_shapes.mjs — verify that the note/text/frame records this skill
  3. // documents are valid against the real tldraw SDK v5 schema.
  4. //
  5. // Usage:
  6. // npm install @tldraw/tlschema
  7. // node validate_shapes.mjs
  8. //
  9. // Exits 0 when all sample records validate, 1 otherwise. No network, no DOM.
  10. import { createTLSchema, toRichText, createShapeId, PageRecordType } from '@tldraw/tlschema'
  11. const schema = createTLSchema()
  12. const shapeRecord = schema.types.shape
  13. const pageId = PageRecordType.createId()
  14. // Complete default prop sets (required when building raw records outside the editor).
  15. const COMPLETE = {
  16. note: {
  17. richText: toRichText(''), color: 'black', labelColor: 'black', size: 'm',
  18. font: 'draw', align: 'middle', verticalAlign: 'middle', growY: 0,
  19. fontSizeAdjustment: 0, url: '', scale: 1, textLastEditedBy: '',
  20. },
  21. text: {
  22. richText: toRichText(''), color: 'black', size: 'm', font: 'draw',
  23. textAlign: 'start', w: 8, scale: 1, autoSize: true,
  24. },
  25. frame: { w: 300, h: 640, name: '', color: 'black' },
  26. }
  27. function makeRecord(type, props, meta = {}, x = 0, y = 0) {
  28. return {
  29. id: createShapeId(), typeName: 'shape', type, parentId: pageId, index: 'a1',
  30. x, y, rotation: 0, isLocked: false, opacity: 1, meta,
  31. props: { ...COMPLETE[type], ...props },
  32. }
  33. }
  34. const cases = [
  35. ['frame', makeRecord('frame', { w: 300, h: 640, name: 'To Do' }, { role: 'column' })],
  36. ['text', makeRecord('text', { richText: toRichText('To Do · WIP 2'), size: 's', color: 'grey', font: 'sans' }, { role: 'count' }, 8, -34)],
  37. ['note', makeRecord('note', { richText: toRichText('Design the thing'), size: 's' }, { role: 'card' }, 20, 48)],
  38. ]
  39. let ok = 0
  40. const validated = []
  41. for (const [name, rec] of cases) {
  42. try {
  43. validated.push(shapeRecord.validate(rec))
  44. console.log(`OK ${name}`)
  45. ok++
  46. } catch (e) {
  47. console.log(`FAIL ${name}: ${String(e.message).split('\n')[0]}`)
  48. }
  49. }
  50. // Round-trip through JSON to mimic file save/load.
  51. let rok = 0
  52. for (const v of validated) {
  53. try { shapeRecord.validate(JSON.parse(JSON.stringify(v))); rok++ } catch { /* counted below */ }
  54. }
  55. console.log(`\n${ok}/${cases.length} shape records valid against the tldraw schema.`)
  56. console.log(`${rok}/${validated.length} survive a JSON round-trip (file save/load).`)
  57. process.exit(ok === cases.length && rok === validated.length ? 0 : 1)