|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#29133] A string-typed field (`BOUNDED_STRING_FIELD_TYPES`) refuses a plain |
| 5 | + * object or an array as its value. |
| 6 | + * |
| 7 | + * Measured on `origin/main` @ `fea2dbb7` before the change: `validateRecord` |
| 8 | + * admitted `{ nested: true }` and `['a', 'b']` on `text` and `textarea`, on |
| 9 | + * insert and on update, because the arm judged `String(value)` while the write |
| 10 | + * carried `value`; the driver then stored the JSON text. The arm now refuses |
| 11 | + * both shapes with the validator's existing `invalid_type` wire code and the |
| 12 | + * ADR-0104 `invalid_value_shape` sentence. Numbers, booleans and a missing |
| 13 | + * value are untouched. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect } from 'vitest'; |
| 17 | +import { BOUNDED_STRING_FIELD_TYPES } from '@objectstack/spec/data'; |
| 18 | +import { validateRecord, validateRecordInScope, ValidationError } from './record-validator.js'; |
| 19 | +import { ObjectQL } from '../engine.js'; |
| 20 | + |
| 21 | +const STRING_TYPES = [...BOUNDED_STRING_FIELD_TYPES]; |
| 22 | + |
| 23 | +function errorsOf( |
| 24 | + schema: { fields: Record<string, any> }, |
| 25 | + data: Record<string, unknown>, |
| 26 | + mode: 'insert' | 'update', |
| 27 | +) { |
| 28 | + try { |
| 29 | + validateRecord(schema, data, mode); |
| 30 | + } catch (e) { |
| 31 | + if (e instanceof ValidationError) return e.fields; |
| 32 | + throw e; |
| 33 | + } |
| 34 | + return []; |
| 35 | +} |
| 36 | + |
| 37 | +describe('#29133 — a string-typed field refuses a plain object or an array', () => { |
| 38 | + it('reads the spec set, which still holds the twelve members this pin was written against', () => { |
| 39 | + // A member joining or leaving the spec set moves this door with it; the |
| 40 | + // count is pinned so a change is seen here, not inferred. |
| 41 | + expect(STRING_TYPES).toHaveLength(12); |
| 42 | + expect(STRING_TYPES).toContain('text'); |
| 43 | + expect(STRING_TYPES).toContain('textarea'); |
| 44 | + }); |
| 45 | + |
| 46 | + for (const type of STRING_TYPES) { |
| 47 | + const schema = { fields: { f: { type, label: 'F' } } }; |
| 48 | + for (const [shape, value, received] of [ |
| 49 | + ['object', { nested: true }, 'an object'], |
| 50 | + ['array', ['a', 'b'], 'an array'], |
| 51 | + ['empty object', {}, 'an object'], |
| 52 | + ['empty array', [], 'an array'], |
| 53 | + ] as const) { |
| 54 | + for (const mode of ['insert', 'update'] as const) { |
| 55 | + it(`${type}: refuses ${shape} on ${mode} as invalid_type`, () => { |
| 56 | + const errs = errorsOf(schema, { f: value }, mode); |
| 57 | + expect(errs).toHaveLength(1); |
| 58 | + expect(errs[0].field).toBe('f'); |
| 59 | + expect(errs[0].code).toBe('invalid_type'); |
| 60 | + expect(errs[0].constraint).toMatchObject({ |
| 61 | + type, |
| 62 | + detail: `expected a string, received ${received}`, |
| 63 | + }); |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + it('says what was wrong in the message: the field, the type and the received shape', () => { |
| 70 | + const errs = errorsOf({ fields: { title: { type: 'text', label: 'Title' } } }, { title: { nested: true } }, 'insert'); |
| 71 | + expect(errs[0].message).toContain('Title'); |
| 72 | + expect(errs[0].message).toContain('text'); |
| 73 | + expect(errs[0].message).toContain('expected a string, received an object'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('refuses the shape on a readonly field too (a shape arm, reached under the readonly scope)', () => { |
| 77 | + const schema = { fields: { stamp: { type: 'text', label: 'Stamp', readonly: true } } }; |
| 78 | + let err: unknown = null; |
| 79 | + try { |
| 80 | + validateRecordInScope(schema, { stamp: ['x'] }, 'insert', 'include'); |
| 81 | + } catch (e) { |
| 82 | + err = e; |
| 83 | + } |
| 84 | + expect(err).toBeInstanceOf(ValidationError); |
| 85 | + expect((err as ValidationError).fields.map((f) => [f.field, f.code])).toEqual([['stamp', 'invalid_type']]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('a filter-operator object keeps its own, earlier sentence', () => { |
| 89 | + const errs = errorsOf({ fields: { title: { type: 'text', label: 'Title' } } }, { title: { $in: ['a'] } }, 'update'); |
| 90 | + expect(errs).toHaveLength(1); |
| 91 | + expect(errs[0].message).toContain('$in is a filter operator, not a value'); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe('#29133 — what the rule leaves alone', () => { |
| 96 | + const schema = { |
| 97 | + fields: { |
| 98 | + title: { type: 'text', label: 'Title', maxLength: 5 }, |
| 99 | + payload: { type: 'json', label: 'Payload' }, |
| 100 | + mail: { type: 'email', label: 'Mail' }, |
| 101 | + }, |
| 102 | + }; |
| 103 | + |
| 104 | + it('a string is admitted, and its bounds still bind', () => { |
| 105 | + expect(errorsOf(schema, { title: 'ok' }, 'insert')).toEqual([]); |
| 106 | + expect(errorsOf(schema, { title: 'too long' }, 'insert')[0].code).toBe('max_length'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('a number and a boolean keep their string reading (out of this rule)', () => { |
| 110 | + expect(errorsOf(schema, { title: 42 }, 'insert')).toEqual([]); |
| 111 | + expect(errorsOf(schema, { title: true }, 'update')).toEqual([]); |
| 112 | + }); |
| 113 | + |
| 114 | + it('null and undefined are unchanged', () => { |
| 115 | + expect(errorsOf(schema, { title: null }, 'update')).toEqual([]); |
| 116 | + expect(errorsOf(schema, { title: undefined }, 'insert')).toEqual([]); |
| 117 | + }); |
| 118 | + |
| 119 | + it('a json field still holds any object', () => { |
| 120 | + expect(errorsOf(schema, { payload: { nested: true } }, 'insert')).toEqual([]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('a format type answers the shape refusal, not its format sentence', () => { |
| 124 | + const errs = errorsOf(schema, { mail: { address: 'a@b.co' } }, 'insert'); |
| 125 | + expect(errs.map((e) => e.code)).toEqual(['invalid_type']); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +/** |
| 130 | + * The end-to-end half: the engine's create, update and multi-update doors |
| 131 | + * reach the rule, answer the VALIDATION_FAILED envelope, and the driver is |
| 132 | + * never written. |
| 133 | + */ |
| 134 | +describe('#29133 — write path end to end: no JSON text reaches the driver', () => { |
| 135 | + async function boot() { |
| 136 | + const engine = new ObjectQL(); |
| 137 | + const writes: unknown[] = []; |
| 138 | + const rows = new Map<string, Record<string, unknown>>([['rec_1', { id: 'rec_1', title: 'orig' }]]); |
| 139 | + engine.registerDriver({ |
| 140 | + name: 'recording', version: '0.0.0', supports: {}, |
| 141 | + async connect() {}, async disconnect() {}, async checkHealth() { return true; }, |
| 142 | + async execute() { return null; }, |
| 143 | + async find() { return Array.from(rows.values()); }, |
| 144 | + async findOne(_o: string, ast: any) { return rows.get(ast?.where?.id) ?? null; }, |
| 145 | + async create(_o: string, data: Record<string, unknown>) { writes.push(data); return { ...data, id: 'x' }; }, |
| 146 | + async update(_o: string, id: string, data: Record<string, unknown>) { writes.push(data); return { ...data, id }; }, |
| 147 | + async updateMany(_o: unknown, _a: unknown, data: unknown) { writes.push(data); return 0; }, |
| 148 | + async delete() { return true; }, async count() { return rows.size; }, |
| 149 | + async bulkCreate() { return []; }, async bulkUpdate() { return []; }, async bulkDelete() {}, |
| 150 | + async beginTransaction() { return { commit: async () => {}, rollback: async () => {} }; }, |
| 151 | + async commit() {}, async rollback() {}, |
| 152 | + } as any, true); |
| 153 | + await engine.init(); |
| 154 | + engine.registry.registerObject({ |
| 155 | + name: 'shape_task', label: 'Task', |
| 156 | + fields: { |
| 157 | + id: { name: 'id', label: 'ID', type: 'text', primaryKey: true }, |
| 158 | + title: { name: 'title', label: 'Title', type: 'text' }, |
| 159 | + }, |
| 160 | + } as any); |
| 161 | + return { engine, writes, rows }; |
| 162 | + } |
| 163 | + |
| 164 | + it('refuses on insert, update and multi-update, and the driver sees no write', async () => { |
| 165 | + const { engine, writes, rows } = await boot(); |
| 166 | + for (const run of [ |
| 167 | + () => engine.insert('shape_task', { title: { nested: true } } as any), |
| 168 | + () => engine.update('shape_task', { title: ['a', 'b'] } as any, { where: { id: 'rec_1' } } as any), |
| 169 | + () => engine.update('shape_task', { title: { nested: true } } as any, { multi: true } as any), |
| 170 | + ]) { |
| 171 | + const err = await run().then(() => null, (e: unknown) => e as ValidationError); |
| 172 | + expect(err).toBeInstanceOf(ValidationError); |
| 173 | + expect(err!.code).toBe('VALIDATION_FAILED'); |
| 174 | + expect(err!.fields.map((f) => [f.field, f.code])).toEqual([['title', 'invalid_type']]); |
| 175 | + } |
| 176 | + expect(writes).toEqual([]); |
| 177 | + expect(rows.get('rec_1')).toEqual({ id: 'rec_1', title: 'orig' }); |
| 178 | + }); |
| 179 | + |
| 180 | + it('a string write on the same path still lands', async () => { |
| 181 | + const { engine, writes } = await boot(); |
| 182 | + await engine.update('shape_task', { title: 'renamed' } as any, { where: { id: 'rec_1' } } as any); |
| 183 | + expect(writes).toEqual([expect.objectContaining({ title: 'renamed' })]); |
| 184 | + }); |
| 185 | +}); |
0 commit comments