mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-13 07:26:26 +00:00
feat: Schema utility for URL parsing (#23043)
This commit is contained in:
parent
674f9cbfcc
commit
fd3d577a8a
2 changed files with 35 additions and 1 deletions
|
@ -1,5 +1,12 @@
|
|||
import { z } from 'zod';
|
||||
import { Json, Json5, LooseArray, LooseRecord, UtcDate } from './schema-utils';
|
||||
import {
|
||||
Json,
|
||||
Json5,
|
||||
LooseArray,
|
||||
LooseRecord,
|
||||
Url,
|
||||
UtcDate,
|
||||
} from './schema-utils';
|
||||
|
||||
describe('util/schema-utils', () => {
|
||||
describe('LooseArray', () => {
|
||||
|
@ -270,4 +277,22 @@ describe('util/schema-utils', () => {
|
|||
expect(() => UtcDate.parse('foobar')).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Url', () => {
|
||||
it('parses valid URLs', () => {
|
||||
const urlStr = 'https://www.example.com/foo/bar?baz=qux';
|
||||
const parsedUrl = Url.parse(urlStr);
|
||||
expect(parsedUrl).toMatchObject({
|
||||
protocol: 'https:',
|
||||
hostname: 'www.example.com',
|
||||
pathname: '/foo/bar',
|
||||
search: '?baz=qux',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an error for invalid URLs', () => {
|
||||
const urlStr = 'invalid-url-string';
|
||||
expect(() => Url.parse(urlStr)).toThrow('Invalid URL');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -223,3 +223,12 @@ export const UtcDate = z
|
|||
}
|
||||
return date;
|
||||
});
|
||||
|
||||
export const Url = z.string().transform((str, ctx): URL => {
|
||||
try {
|
||||
return new URL(str);
|
||||
} catch (e) {
|
||||
ctx.addIssue({ code: 'custom', message: 'Invalid URL' });
|
||||
return z.NEVER;
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue