import { readFile } from 'fs/promises'; import path from 'path'; import { AUTHOR_SCHEMA } from '~/author/author.schema'; import { YAMLDocumentValidator, YAMLDocumentInput } from '../validator'; async function createValidatorSource( filepath: string, ): Promise { const content = await readFile(path.resolve(__dirname, filepath), 'utf8'); return { filepath, content, }; } describe('Validator', function () { it('handles of a malformed top-level scalar', async function () { const source = await createValidatorSource( './fixtures/slug-mixedcase.yaml', ); const validator = new YAMLDocumentValidator(source, AUTHOR_SCHEMA); const issues = await validator.run(); expect(issues[0]).toEqual( expect.objectContaining({ filepath: './fixtures/slug-mixedcase.yaml', line: 2, col: 7, message: 'slug must be a lowercase string', }), ); }); it('handles of a missing top-level scalar', async function () { const source = await createValidatorSource('./fixtures/email-missing.yaml'); const validator = new YAMLDocumentValidator(source, AUTHOR_SCHEMA); const issues = await validator.run(); expect(issues[0]).toEqual( expect.objectContaining({ filepath: './fixtures/email-missing.yaml', line: 2, col: 1, message: 'email is a required field', }), ); }); it('handles of a sequence entry', async function () { const source = await createValidatorSource( './fixtures/series-mixedcase.yaml', ); const validator = new YAMLDocumentValidator(source, AUTHOR_SCHEMA); const issues = await validator.run(); expect(issues[0]).toEqual( expect.objectContaining({ filepath: './fixtures/series-mixedcase.yaml', line: 14, col: 5, message: 'series[0] must be a lowercase string', }), ); }); it('handles of a malformed property of an map in a sequence', async function () { const source = await createValidatorSource( './fixtures/contacts-nonurl.yaml', ); const validator = new YAMLDocumentValidator(source, AUTHOR_SCHEMA); const issues = await validator.run(); expect(issues[0]).toEqual( expect.objectContaining({ filepath: './fixtures/contacts-nonurl.yaml', line: 10, col: 11, message: 'contacts[1].link must be a valid URL', }), ); }); it('handles of multiple validation errors in the document', async function () { const source = await createValidatorSource( './fixtures/slug-nonens-series-mixedcase.yaml', ); const validator = new YAMLDocumentValidator(source, AUTHOR_SCHEMA); const issues = await validator.run({ abortEarly: false }); expect(issues[0]).toEqual( expect.objectContaining({ filepath: './fixtures/slug-nonens-series-mixedcase.yaml', line: 2, col: 7, message: 'slug must match the following: "/^[a-z0-9-]+\\.eth$/"', }), ); expect(issues[1]).toEqual( expect.objectContaining({ filepath: './fixtures/slug-nonens-series-mixedcase.yaml', line: 6, col: 5, message: 'series[0] must be a lowercase string', }), ); expect(issues[2]).toEqual( expect.objectContaining({ filepath: './fixtures/slug-nonens-series-mixedcase.yaml', line: 6, col: 5, message: 'series[0] must match the following: "/^[a-z0-9-]+$/"', }), ); }); });