diff --git a/src/rule/url.ts b/src/rule/url.ts index 34a5b1d..5acc0a4 100644 --- a/src/rule/url.ts +++ b/src/rule/url.ts @@ -53,7 +53,7 @@ export default () => { const tld = `(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))`; const port = '(?::\\d{2,5})?'; const path = '(?:[/?#][^\\s"]*)?'; - const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ipv4}|${ipv6}|${host}${domain}${tld})${port}${path}`; + const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ipv4}|${ipv6}|${host}(?:${domain}${tld})?)${port}${path}`; urlReg = new RegExp(`(?:^${regex}$)`, 'i'); return urlReg; }; diff --git a/tests/url.spec.ts b/tests/url.spec.ts index 763d38c..49d7426 100644 --- a/tests/url.spec.ts +++ b/tests/url.spec.ts @@ -33,6 +33,39 @@ describe('url', () => { ); }); + it('works for single-label host url', done => { + new Schema({ + v: { + type: 'url', + }, + }).validate( + { + v: 'http://fastapi:3000/mcp', + }, + errors => { + expect(errors).toBe(null); + done(); + }, + ); + }); + + it('rejects invalid single-label host url', done => { + new Schema({ + v: { + type: 'url', + }, + }).validate( + { + v: 'http://-fastapi:3000/mcp', + }, + errors => { + expect(errors).toHaveLength(1); + expect(errors[0].message).toBe('v is not a valid url'); + done(); + }, + ); + }); + it('works for required empty string', done => { new Schema({ v: {