Skip to content

Instantly share code, notes, and snippets.

@StephanMeijer
Created July 12, 2023 19:51
Show Gist options
  • Save StephanMeijer/1711349b7d738ad233825feb1683f24f to your computer and use it in GitHub Desktop.
Save StephanMeijer/1711349b7d738ad233825feb1683f24f to your computer and use it in GitHub Desktop.
import { describe, test } from '@jest/globals';
import { SchemaMatcher } from '../__testUtils__/zod/SchemaMatcher.js';
import { ChecksumSchema } from '../../src/checksum/Checksum.js';
import { HashAlgorithmType } from '../../src/checksum/algorithms/HashAlgorithmInterface.js';
import { ZodError } from 'zod';
describe('ChecksumSchema', () => {
const expectSchema = new SchemaMatcher(ChecksumSchema);
test(`Schema (valid)`, async () => {
expectSchema
.parsing({
algorithm: HashAlgorithmType.SHA3_512,
hash: '123456',
})
.toSucceed()
.andEqualInputValue();
});
test(`schema (invalid algorithm)`, async () => {
expectSchema
.parsing({
algorithm: 'invalid',
hash: '123456',
})
.toFail()
.withError(
new ZodError([
{
received: 'invalid',
code: 'invalid_enum_value',
options: ['blake2b512', 'sha1', 'sha512', 'sha3-512'],
path: ['algorithm'],
message:
"Invalid enum value. Expected 'blake2b512' | 'sha1' | 'sha512' | 'sha3-512', received 'invalid'",
},
]),
);
});
test(`schema (missing property hash)`, async () => {
expectSchema
.parsing({
algorithm: HashAlgorithmType.SHA3_512,
})
.toFail()
.withError(
new ZodError([
{
code: 'invalid_type',
expected: 'string',
received: 'undefined',
path: ['hash'],
message: 'Required',
},
]),
);
});
test(`schema (missing property 'algorithm')`, async () => {
expectSchema
.parsing({
hash: '123456',
})
.toFail()
.withError(
new ZodError([
{
expected:
"'blake2b512' | 'sha1' | 'sha512' | 'sha3-512'" as 'string',
received: 'undefined',
code: 'invalid_type',
path: ['algorithm'],
message: 'Required',
},
]),
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment