/
githubmirror
/
tldraw
Обзор
Документация
Войти
/
githubmirror
/
tldraw
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/validate/src/test/model.test.ts
34 строки
1 KB
Steve Ruiz
test(validate): rebuild unit tests around a behavior specification (#9177)
13 июн 2026, 00:40
Не верифицирован
13 июн 2026, 00:40
b894111
Код
Авторство
О чём код?
import * as T from '../lib/validation' describe('§14 Models', () => { it('[M1] model prefixes the model name onto validation failures', () => { const shape = T.model('shape', T.object({ id: T.string, x: T.number, y: T.number })) const value = { id: 'abc123', x: 132, y: 0 } expect(shape.validate(value)).toBe(value) expect(() => shape.validate({ id: 'abc123', x: 132, y: NaN })).toThrow( 'At shape.y: Expected a number, got NaN' ) expect(() => T.model( 'shape', T.object({ id: T.string, color: T.setEnum(new Set(['red', 'green', 'blue'])) }) ).validate({ id: 'abc13', color: 'rubbish' }) ).toThrow('At shape.color: Expected "red" or "green" or "blue", got rubbish') }) it('[M2] known-good validation delegates to the inner validator with the same prefixing', () => { const user = T.model('user', T.object({ id: T.string, n: T.number })) const knownGood = user.validate({ id: 'x', n: 1 }) expect(user.validateUsingKnownGoodVersion(knownGood, { id: 'x', n: 1 })).toBe(knownGood) const next = { id: 'x', n: 2 } expect(user.validateUsingKnownGoodVersion(knownGood, next)).toBe(next) expect(() => user.validateUsingKnownGoodVersion(knownGood, { id: 'x', n: 'bad' })).toThrow( 'At user.n: Expected number, got a string' ) }) })