|
| 1 | +import { z } from 'zod' |
| 2 | +import { test } from '@japa/runner' |
| 3 | +import { is } from '@julr/utils/is' |
| 4 | + |
| 5 | +import { errors } from '../../src/errors.js' |
| 6 | +import { CacheFactory } from '../../factories/cache_factory.js' |
| 7 | + |
| 8 | +test.group('Validation', () => { |
| 9 | + test('works with standard schema', async ({ assert }) => { |
| 10 | + const { cache } = new CacheFactory().withL1L2Config().create() |
| 11 | + |
| 12 | + await assert.rejects(async () => { |
| 13 | + await cache.set({ |
| 14 | + key: 'foo', |
| 15 | + value: { foo: 3 }, |
| 16 | + validate: z.object({ foo: z.string(), bar: z.number() }), |
| 17 | + }) |
| 18 | + // @ts-ignore |
| 19 | + }, errors.E_VALIDATION_ERROR) |
| 20 | + }) |
| 21 | + |
| 22 | + test('works with fn', async ({ assert }) => { |
| 23 | + const { cache } = new CacheFactory().withL1L2Config().create() |
| 24 | + |
| 25 | + await assert.rejects(async () => { |
| 26 | + await cache.set({ |
| 27 | + key: 'foo', |
| 28 | + value: { foo: 3 }, |
| 29 | + validate: (value) => { |
| 30 | + if (!is.plainObject(value)) { |
| 31 | + throw new TypeError('Value must be an object') |
| 32 | + } |
| 33 | + |
| 34 | + if (typeof value.foo !== 'string') { |
| 35 | + throw new TypeError('Value must be a string') |
| 36 | + } |
| 37 | + }, |
| 38 | + }) |
| 39 | + // @ts-ignore |
| 40 | + }, errors.E_VALIDATION_ERROR) |
| 41 | + }) |
| 42 | + |
| 43 | + test('doesnt throw when validation passes', async () => { |
| 44 | + const { cache } = new CacheFactory().withL1L2Config().create() |
| 45 | + |
| 46 | + await cache.set({ |
| 47 | + key: 'foo', |
| 48 | + value: { foo: '3' }, |
| 49 | + validate: z.object({ foo: z.string() }), |
| 50 | + }) |
| 51 | + |
| 52 | + await cache.set({ |
| 53 | + key: 'foo', |
| 54 | + value: { foo: '3' }, |
| 55 | + validate: (value) => { |
| 56 | + if (!is.plainObject(value)) throw new TypeError('Value must be an object') |
| 57 | + if (typeof value.foo !== 'string') throw new TypeError('Value must be a string') |
| 58 | + }, |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + test('cache object transformed by schema', async ({ assert }) => { |
| 63 | + const { cache } = new CacheFactory().withL1L2Config().create() |
| 64 | + |
| 65 | + await cache.set({ |
| 66 | + key: 'foo', |
| 67 | + value: { foo: 'wesh' }, |
| 68 | + validate: z.object({ foo: z.string().toUpperCase() }), |
| 69 | + }) |
| 70 | + |
| 71 | + const r1 = await cache.get({ key: 'foo' }) |
| 72 | + assert.deepEqual(r1, { foo: 'WESH' }) |
| 73 | + }) |
| 74 | + |
| 75 | + test('works with getOrSet', async ({ assert }) => { |
| 76 | + const { cache } = new CacheFactory().withL1L2Config().create() |
| 77 | + |
| 78 | + await assert.rejects(async () => { |
| 79 | + await cache.getOrSet({ |
| 80 | + key: 'foo', |
| 81 | + factory: () => ({ foo: 3 }), |
| 82 | + validate: z.object({ foo: z.string() }), |
| 83 | + }) |
| 84 | + // @ts-ignore |
| 85 | + }, errors.E_VALIDATION_ERROR) |
| 86 | + |
| 87 | + const r1 = await cache.get({ key: 'foo' }) |
| 88 | + assert.isUndefined(r1) |
| 89 | + |
| 90 | + await cache.getOrSet({ |
| 91 | + key: 'foo', |
| 92 | + factory: () => ({ foo: 'foo' }), |
| 93 | + validate: z.object({ foo: z.string() }), |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + test('validate when fetching from getOrSet', async () => { |
| 98 | + const { cache } = new CacheFactory().withL1L2Config().create() |
| 99 | + |
| 100 | + await cache.set({ key: 'foo', value: { foo: 3 } }) |
| 101 | + |
| 102 | + await cache.getOrSet({ |
| 103 | + key: 'foo', |
| 104 | + factory: () => ({ foo: 'foo' }), |
| 105 | + validate: z.object({ foo: z.string() }), |
| 106 | + }) |
| 107 | + }) |
| 108 | +}) |
0 commit comments