@streamscloud/kit 0.36.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- export type ValidationError = 'NotString' | 'Empty' | 'ControlChars' | 'DisallowedScheme' | 'MissingHost' | 'EmbeddedCredentials' | 'Relative' | 'BareRelative';
1
+ export type ValidationError = 'NotString' | 'Empty' | 'ControlChars' | 'DisallowedScheme' | 'MissingHost' | 'UnknownHost' | 'EmbeddedCredentials' | 'Relative' | 'BareRelative';
2
2
  type DetailedResult = {
3
3
  valid: true;
4
4
  } | {
@@ -8,6 +8,10 @@ type DetailedResult = {
8
8
  export type ValidateOptions = {
9
9
  allowRelative?: 'all' | 'prefixed';
10
10
  allowedSchemes?: Set<string>;
11
+ /** Rejects a syntactically valid but hostless-looking host (`https://sdf`) — requires a public label+TLD shape, `localhost`, an IPv4 literal, or a bracketed IPv6 literal. */
12
+ requireNamedHost?: boolean;
13
+ /** With `requireNamedHost`, also accepts `localhost` / IPv4 / bracketed IPv6 as known hosts. */
14
+ allowLocalHosts?: boolean;
11
15
  };
12
16
  export declare const validateHref: (input: unknown, opts?: ValidateOptions) => DetailedResult;
13
17
  export {};
@@ -1,4 +1,9 @@
1
1
  const DEFAULT_ALLOWED_SCHEMES = new Set(['http:', 'https:', 'mailto:', 'tel:']);
2
+ // Shape check only, not full RFC 5891 punycode validation — a non-ICANN xn-- label (e.g. xn--1234) still passes.
3
+ const PUBLIC_HOST = /^[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,}|xn--[a-z0-9-]{2,})$/i;
4
+ const IPV4_HOST = /^\d{1,3}(\.\d{1,3}){3}$/;
5
+ // A leading '[' is safe to trust here only because `new URL()` already parsed and validated the bracketed IPv6 literal.
6
+ const isLocalHost = (hostname) => hostname === 'localhost' || IPV4_HOST.test(hostname) || hostname.startsWith('[');
2
7
  export const validateHref = (input, opts = {}) => {
3
8
  if (typeof input !== 'string') {
4
9
  return { valid: false, error: 'NotString' };
@@ -25,6 +30,12 @@ export const validateHref = (input, opts = {}) => {
25
30
  if (u.username || u.password) {
26
31
  return { valid: false, error: 'EmbeddedCredentials' };
27
32
  }
33
+ if (opts.requireNamedHost && (proto === 'http:' || proto === 'https:')) {
34
+ const knownHost = PUBLIC_HOST.test(u.hostname) || (!!opts.allowLocalHosts && isLocalHost(u.hostname));
35
+ if (!knownHost) {
36
+ return { valid: false, error: 'UnknownHost' };
37
+ }
38
+ }
28
39
  return { valid: true };
29
40
  }
30
41
  catch {
@@ -1,6 +1,7 @@
1
- export type { TextValidation, TextWithFormatValidation, NumberValidation, MinNumberValidation, ArrayValidation } from './types';
1
+ export type { TextValidation, TextWithFormatValidation, NumberValidation, MinNumberValidation, ArrayValidation, UrlValidation } from './types';
2
2
  export { emailValidationSchema, nullableEmailValidationSchema } from './email-validation';
3
3
  export { textValidationSchema, formattedTextValidationSchema } from './text-validations';
4
4
  export { handleValidationSchema } from './handle-validations';
5
5
  export { numberValidationSchema, minNumberValidationSchema } from './number-validations';
6
6
  export { arrayValidationSchema } from './array-validations';
7
+ export { urlValidationSchema } from './url-validation';
@@ -3,3 +3,4 @@ export { textValidationSchema, formattedTextValidationSchema } from './text-vali
3
3
  export { handleValidationSchema } from './handle-validations';
4
4
  export { numberValidationSchema, minNumberValidationSchema } from './number-validations';
5
5
  export { arrayValidationSchema } from './array-validations';
6
+ export { urlValidationSchema } from './url-validation';
@@ -5,6 +5,16 @@ export type TextValidation = {
5
5
  export type TextWithFormatValidation = TextValidation & {
6
6
  format: string;
7
7
  };
8
+ export type UrlValidation = {
9
+ /** @default false */
10
+ required?: boolean;
11
+ maxLength?: number;
12
+ schemes?: Set<string>;
13
+ /** @default true */
14
+ requireNamedHost?: boolean;
15
+ /** @default true */
16
+ allowLocalHosts?: boolean;
17
+ };
8
18
  export type NumberValidation = {
9
19
  minValue: number | null;
10
20
  maxValue: number | null;
@@ -0,0 +1,3 @@
1
+ import type { UrlValidation } from './types';
2
+ import * as yup from 'yup';
3
+ export declare const urlValidationSchema: (rules?: UrlValidation) => yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
@@ -0,0 +1,13 @@
1
+ import { validateHref } from '../../utils';
2
+ import { ValidationLocalization } from './validation-localization';
3
+ import * as yup from 'yup';
4
+ const DEFAULT_SCHEMES = new Set(['http:', 'https:']);
5
+ export const urlValidationSchema = (rules = {}) => {
6
+ const msg = new ValidationLocalization();
7
+ const allowedSchemes = rules.schemes ?? DEFAULT_SCHEMES;
8
+ const requireNamedHost = rules.requireNamedHost ?? true;
9
+ const allowLocalHosts = rules.allowLocalHosts ?? true;
10
+ const schema = yup.string().test('url', msg.url, (value) => !value || validateHref(value, { allowedSchemes, requireNamedHost, allowLocalHosts }).valid);
11
+ const withMaxLength = rules.maxLength === undefined ? schema : schema.max(rules.maxLength, msg.maxLength(rules.maxLength));
12
+ return rules.required ? withMaxLength.required(msg.required) : withMaxLength;
13
+ };
@@ -8,4 +8,5 @@ export declare class ValidationLocalization {
8
8
  get minItems(): (count: number) => string;
9
9
  get maxItems(): (count: number) => string;
10
10
  get badFormat(): string;
11
+ get url(): string;
11
12
  }
@@ -27,6 +27,9 @@ export class ValidationLocalization {
27
27
  get badFormat() {
28
28
  return loc.badFormat[AppLocale.current];
29
29
  }
30
+ get url() {
31
+ return loc.url[AppLocale.current];
32
+ }
30
33
  }
31
34
  const loc = {
32
35
  required: {
@@ -64,5 +67,9 @@ const loc = {
64
67
  badFormat: {
65
68
  en: 'Invalid format',
66
69
  no: 'Ugyldig format'
70
+ },
71
+ url: {
72
+ en: 'Please enter a valid URL',
73
+ no: 'Vennligst skriv inn en gyldig URL'
67
74
  }
68
75
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamscloud/kit",
3
- "version": "0.36.0",
3
+ "version": "0.37.0",
4
4
  "author": "StreamsCloud",
5
5
  "repository": {
6
6
  "type": "git",