ts-fortress 8.3.0 → 8.4.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.
@@ -47,6 +47,7 @@ export { email } from './predefined/brand/string/email.mjs';
47
47
  export { iso8601 } from './predefined/brand/string/iso-8601.mjs';
48
48
  export { jsonString } from './predefined/brand/string/json-string.mjs';
49
49
  export { nonEmptyString } from './predefined/brand/string/non-empty-string.mjs';
50
+ export { uri } from './predefined/brand/string/uri.mjs';
50
51
  export { uuid, uuidV4, uuidV6, uuidV7 } from './predefined/brand/string/uuid.mjs';
51
52
  export { int8 } from './predefined/int8.mjs';
52
53
  export { JsonObject, JsonPrimitive, JsonValue } from './predefined/json.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"entry-point.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"entry-point.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/index.mjs CHANGED
@@ -46,6 +46,7 @@ export { email } from './predefined/brand/string/email.mjs';
46
46
  export { iso8601 } from './predefined/brand/string/iso-8601.mjs';
47
47
  export { jsonString } from './predefined/brand/string/json-string.mjs';
48
48
  export { nonEmptyString } from './predefined/brand/string/non-empty-string.mjs';
49
+ export { uri } from './predefined/brand/string/uri.mjs';
49
50
  export { uuid, uuidV4, uuidV6, uuidV7 } from './predefined/brand/string/uuid.mjs';
50
51
  export { int8 } from './predefined/int8.mjs';
51
52
  export { JsonObject, JsonPrimitive, JsonValue } from './predefined/json.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -24,5 +24,6 @@ export { email } from './string/email.mjs';
24
24
  export { iso8601 } from './string/iso-8601.mjs';
25
25
  export { jsonString } from './string/json-string.mjs';
26
26
  export { nonEmptyString } from './string/non-empty-string.mjs';
27
+ export { uri } from './string/uri.mjs';
27
28
  export { uuid, uuidV4, uuidV6, uuidV7 } from './string/uuid.mjs';
28
29
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -2,5 +2,6 @@ export * from './email.mjs';
2
2
  export * from './iso-8601.mjs';
3
3
  export * from './json-string.mjs';
4
4
  export * from './non-empty-string.mjs';
5
+ export * from './uri.mjs';
5
6
  export * from './uuid.mjs';
6
7
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/predefined/brand/string/index.mts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/predefined/brand/string/index.mts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC"}
@@ -2,5 +2,6 @@ export { email } from './email.mjs';
2
2
  export { iso8601 } from './iso-8601.mjs';
3
3
  export { jsonString } from './json-string.mjs';
4
4
  export { nonEmptyString } from './non-empty-string.mjs';
5
+ export { uri } from './uri.mjs';
5
6
  export { uuid, uuidV4, uuidV6, uuidV7 } from './uuid.mjs';
6
7
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
@@ -0,0 +1,10 @@
1
+ import { type Brand } from 'ts-type-forge';
2
+ import { type Type } from '../../../type.mjs';
3
+ export type Uri = Brand<string, 'Uri' | 'NonEmptyString'>;
4
+ /**
5
+ * @link https://url.spec.whatwg.org/
6
+ */
7
+ export declare const uri: (options?: Partial<Readonly<{
8
+ defaultValue: string;
9
+ }>>) => Type<Uri>;
10
+ //# sourceMappingURL=uri.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uri.d.mts","sourceRoot":"","sources":["../../../../src/predefined/brand/string/uri.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,eAAe,CAAC;AAG3C,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAG9C,MAAM,MAAM,GAAG,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,GAAG,gBAAgB,CAAC,CAAC;AAE1D;;GAEG;AACH,eAAO,MAAM,GAAG,GACd,UAAU,OAAO,CACf,QAAQ,CAAC;IACP,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC,CACH,KACA,IAAI,CAAC,GAAG,CAOP,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { brand } from '../../../brand/brand.mjs';
2
+ import 'ts-data-forge';
3
+ import '@sindresorhus/is';
4
+ import '../../../other-types/unknown.mjs';
5
+ import { string } from '../../../primitives/string.mjs';
6
+
7
+ /**
8
+ * @link https://url.spec.whatwg.org/
9
+ */
10
+ const uri = (options) => brand({
11
+ baseType: string(options?.defaultValue ?? defaultUri),
12
+ is: isUri,
13
+ defaultValue: options?.defaultValue ?? defaultUri,
14
+ brandKeys: ['Uri', 'NonEmptyString'],
15
+ typeName: 'Uri',
16
+ });
17
+ const defaultUri = 'https://example.com';
18
+ const isUri = (s) => URL.canParse(s);
19
+
20
+ export { uri };
21
+ //# sourceMappingURL=uri.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uri.mjs","sources":["../../../../src/predefined/brand/string/uri.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;AAQA;;AAEG;AACI,MAAM,GAAG,GAAG,CACjB,OAIC,KAED,KAAK,CAAC;IACJ,QAAQ,EAAE,MAAM,CAAC,OAAO,EAAE,YAAY,IAAI,UAAU,CAAC;AACrD,IAAA,EAAE,EAAE,KAAK;AACT,IAAA,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,UAAU;AACjD,IAAA,SAAS,EAAE,CAAC,KAAK,EAAE,gBAAgB,CAAC;AACpC,IAAA,QAAQ,EAAE,KAAK;AAChB,CAAA;AAEH,MAAM,UAAU,GAAG,qBAAqB;AAExC,MAAM,KAAK,GAAG,CAAC,CAAS,KAAe,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC;;;;"}
@@ -24,6 +24,7 @@ export { email } from './brand/string/email.mjs';
24
24
  export { iso8601 } from './brand/string/iso-8601.mjs';
25
25
  export { jsonString } from './brand/string/json-string.mjs';
26
26
  export { nonEmptyString } from './brand/string/non-empty-string.mjs';
27
+ export { uri } from './brand/string/uri.mjs';
27
28
  export { uuid, uuidV4, uuidV6, uuidV7 } from './brand/string/uuid.mjs';
28
29
  export { int8 } from './int8.mjs';
29
30
  export { JsonObject, JsonPrimitive, JsonValue } from './json.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-fortress",
3
- "version": "8.3.0",
3
+ "version": "8.4.0",
4
4
  "private": false,
5
5
  "keywords": [
6
6
  "typescript"
@@ -2,4 +2,5 @@ export * from './email.mjs';
2
2
  export * from './iso-8601.mjs';
3
3
  export * from './json-string.mjs';
4
4
  export * from './non-empty-string.mjs';
5
+ export * from './uri.mjs';
5
6
  export * from './uuid.mjs';
@@ -69,9 +69,15 @@ describe('nonEmptyString with extra constraints', () => {
69
69
  assert.isFalse(t.is('')); // empty string is still rejected
70
70
  });
71
71
 
72
+ test('throws when the default value violates a nonEmpty constraint', () => {
73
+ expect(() => nonEmptyString('')).toThrow(
74
+ /^defaultValue "" for string does not satisfy the constraint nonempty = true$/u,
75
+ );
76
+ });
77
+
72
78
  test('throws when the default value violates a forwarded constraint', () => {
73
79
  expect(() => nonEmptyString('ab', { minLength: 3 })).toThrow(
74
- 'minLength = 3',
80
+ /^defaultValue "ab" for string does not satisfy the constraint minLength = 3$/u,
75
81
  );
76
82
  });
77
83
 
@@ -0,0 +1,35 @@
1
+ import { type Brand } from 'ts-type-forge';
2
+ import { brand } from '../../../brand/index.mjs';
3
+ import { string } from '../../../primitives/index.mjs';
4
+ import { type Type } from '../../../type.mjs';
5
+
6
+ // A valid URI always has a non-empty scheme, so it also satisfies `NonEmptyString`.
7
+ export type Uri = Brand<string, 'Uri' | 'NonEmptyString'>;
8
+
9
+ /**
10
+ * @link https://url.spec.whatwg.org/
11
+ */
12
+ export const uri = (
13
+ options?: Partial<
14
+ Readonly<{
15
+ defaultValue: string;
16
+ }>
17
+ >,
18
+ ): Type<Uri> =>
19
+ brand({
20
+ baseType: string(options?.defaultValue ?? defaultUri),
21
+ is: isUri,
22
+ defaultValue: options?.defaultValue ?? defaultUri,
23
+ brandKeys: ['Uri', 'NonEmptyString'],
24
+ typeName: 'Uri',
25
+ });
26
+
27
+ const defaultUri = 'https://example.com';
28
+
29
+ const isUri = (s: string): s is Uri => URL.canParse(s);
30
+
31
+ if (import.meta.vitest !== undefined) {
32
+ test('defaultUri', () => {
33
+ assert.isTrue(isUri(defaultUri));
34
+ });
35
+ }
@@ -0,0 +1,79 @@
1
+ import { expectType, Result } from 'ts-data-forge';
2
+ import { type NonEmptyString } from 'ts-type-forge';
3
+ import { type TypeOf } from '../../../type.mjs';
4
+ import { validationErrorsToMessages } from '../../../utils/index.mjs';
5
+ import { uri } from './uri.mjs';
6
+
7
+ const validSamples = [
8
+ 'https://example.com',
9
+ 'https://example.com/path?query=1#fragment',
10
+ 'http://user:pass@example.com:8080/path',
11
+ 'ftp://ftp.is.co.za/rfc/rfc1808.txt',
12
+ 'mailto:foo@bar.com',
13
+ 'urn:isbn:0451450523',
14
+ 'file:///etc/hosts',
15
+ 'tel:+1-816-555-1212',
16
+ ] as const satisfies readonly string[];
17
+
18
+ const invalidSamples = [
19
+ '',
20
+ 'not a uri',
21
+ '://missing-scheme',
22
+ 'relative/path',
23
+ '/absolute/path',
24
+ '1http://example.com',
25
+ ] as const satisfies readonly string[];
26
+
27
+ describe(uri, () => {
28
+ const baseType = uri();
29
+
30
+ type UriType = TypeOf<typeof baseType>;
31
+
32
+ expectType<UriType, string>('<=');
33
+
34
+ expectType<UriType, NonEmptyString>('<=');
35
+
36
+ expectType<typeof baseType.defaultValue, UriType>('=');
37
+
38
+ test('provides https://example.com as default', () => {
39
+ expect(baseType.defaultValue).toBe('https://example.com');
40
+ });
41
+
42
+ test.each(validSamples)('accepts %s', (u) => {
43
+ assert.isTrue(baseType.is(u));
44
+ });
45
+
46
+ test.each(invalidSamples)('rejects %s', (u) => {
47
+ assert.isFalse(baseType.is(u));
48
+ });
49
+
50
+ test('validate surfaces details for invalid strings', () => {
51
+ const result = baseType.validate('not a uri');
52
+
53
+ assert.isTrue(Result.isErr(result));
54
+
55
+ if (!Result.isErr(result)) {
56
+ throw new Error('Expected validation failure');
57
+ }
58
+
59
+ assert.deepStrictEqual(result.value, [
60
+ {
61
+ path: [],
62
+ actualValue: 'not a uri',
63
+ expectedType: 'Uri',
64
+ typeName: 'Uri',
65
+ details: undefined,
66
+ },
67
+ ]);
68
+
69
+ assert.deepStrictEqual(validationErrorsToMessages(result.value), [
70
+ 'Error: expected <Uri> type but <string> type value "not a uri" was passed.',
71
+ ]);
72
+ });
73
+
74
+ test('accepts a custom default value', () => {
75
+ const customType = uri({ defaultValue: 'mailto:foo@bar.com' });
76
+
77
+ expect(customType.defaultValue).toBe('mailto:foo@bar.com');
78
+ });
79
+ });