ts-fortress 8.3.0 → 8.5.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.
@@ -4,7 +4,7 @@ export type { Brand } from 'ts-type-forge';
4
4
  type ArrayToUnion<A extends readonly unknown[]> = A extends readonly [] ? never : A[number];
5
5
  export declare const brand: <const A extends StrictExtract<Primitive, string | number | bigint | boolean>, const BrandTrueKeys extends readonly string[], const BrandFalseKeys extends readonly string[] = readonly []>({ baseType, brandFalseKeys, brandKeys, defaultValue, is, typeName, }: Readonly<{
6
6
  baseType: Type<A>;
7
- is: (a: A) => a is Brand<A, ArrayToUnion<BrandTrueKeys>, ArrayToUnion<BrandFalseKeys>>;
7
+ is?: (a: A) => a is Brand<A, ArrayToUnion<BrandTrueKeys>, ArrayToUnion<BrandFalseKeys>>;
8
8
  defaultValue: A;
9
9
  typeName?: string;
10
10
  brandKeys: BrandTrueKeys;
@@ -1 +1 @@
1
- {"version":3,"file":"brand.d.mts","sourceRoot":"","sources":["../../src/brand/brand.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,KAAK,EAAE,KAAK,SAAS,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAC/E,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,YAAY,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAE3C,KAAK,YAAY,CAAC,CAAC,SAAS,SAAS,OAAO,EAAE,IAAI,CAAC,SAAS,SAAS,EAAE,GACnE,KAAK,GACL,CAAC,CAAC,MAAM,CAAC,CAAC;AAEd,eAAO,MAAM,KAAK,GAChB,KAAK,CAAC,CAAC,SAAS,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAC5E,KAAK,CAAC,aAAa,SAAS,SAAS,MAAM,EAAE,EAC7C,KAAK,CAAC,cAAc,SAAS,SAAS,MAAM,EAAE,GAAG,SAAS,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AE,CACH,CAAC,EAAE,CAAC,KACD,CAAC,IAAI,KAAK,CAAC,CAAC,EAAE,YAAY,CAAC,aAAa,CAAC,EAAE,YAAY,CAAC,cAAc,CAAC,CAAC,CAAC;IAC9E,YAAY,EAAE,CAAC,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,aAAa,CAAC;IACzB,cAAc,CAAC,EAAE,cAAc,CAAC;CACjC,CAAC,KAAG,IAAI,CACP,KAAK,CAAC,CAAC,EAAE,YAAY,CAAC,aAAa,CAAC,EAAE,YAAY,CAAC,cAAc,CAAC,CAAC,CAmDpE,CAAC"}
@@ -2,7 +2,8 @@ import { pipe, Result } from 'ts-data-forge';
2
2
  import { createType } from '../utils/create-type.mjs';
3
3
 
4
4
  const brand = ({ baseType, brandFalseKeys, brandKeys, defaultValue, is, typeName, }) => {
5
- if (!is(defaultValue)) {
5
+ const isFilled = is ?? ((_a) => true);
6
+ if (!isFilled(defaultValue)) {
6
7
  throw new Error(`defaultValue ${defaultValue} doesn't pass \`is\` function`);
7
8
  }
8
9
  const brandKeysStr = [
@@ -14,7 +15,7 @@ const brand = ({ baseType, brandFalseKeys, brandKeys, defaultValue, is, typeName
14
15
  .map(baseType.validate)
15
16
  .map((res) => Result.isErr(res)
16
17
  ? res
17
- : is(res.value)
18
+ : isFilled(res.value)
18
19
  ? Result.ok(res.value)
19
20
  : Result.err([
20
21
  {
@@ -1 +1 @@
1
- {"version":3,"file":"brand.mjs","sources":["../../src/brand/brand.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAWO,MAAM,KAAK,GAAG,CAInB,EACA,QAAQ,EACR,cAAc,EACd,SAAS,EACT,YAAY,EACZ,EAAE,EACF,QAAQ,GAUR,KAEE;AAGF,IAAA,IAAI,CAAC,EAAE,CAAC,YAAY,CAAC,EAAE;AACrB,QAAA,MAAM,IAAI,KAAK,CACb,gBAAgB,YAAY,CAAA,6BAAA,CAA+B,CAC5D;IACH;AAEA,IAAA,M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AEX,IAAA,OAAO,UAAU,CAAC;AAChB,QAAA,QAAQ,EAAE,cAAc;QACxB,YAAY;QACZ,QAAQ;AACT,KAAA,CAAC;AACJ;;;;"}
@@ -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,23 @@
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
+ // `URL.canParse` trims leading/trailing ASCII whitespace per the WHATWG URL parser, so
19
+ // surrounding whitespace must be rejected explicitly to avoid accepting e.g. " https://example.com ".
20
+ const isUri = (s) => s === s.trim() && URL.canParse(s);
21
+
22
+ export { uri };
23
+ //# 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;AACA;AACA,MAAM,KAAK,GAAG,CAAC,CAAS,KAAe,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,IAAI,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.5.0",
4
4
  "private": false,
5
5
  "keywords": [
6
6
  "typescript"
@@ -22,7 +22,7 @@ export const brand = <
22
22
  typeName,
23
23
  }: Readonly<{
24
24
  baseType: Type<A>;
25
- is: (
25
+ is?: (
26
26
  a: A,
27
27
  ) => a is Brand<A, ArrayToUnion<BrandTrueKeys>, ArrayToUnion<BrandFalseKeys>>;
28
28
  defaultValue: A;
@@ -34,7 +34,9 @@ export const brand = <
34
34
  > => {
35
35
  type T = Brand<A, ArrayToUnion<BrandTrueKeys>, ArrayToUnion<BrandFalseKeys>>;
36
36
 
37
- if (!is(defaultValue)) {
37
+ const isFilled: (a: A) => a is T = is ?? ((_a): _a is T => true);
38
+
39
+ if (!isFilled(defaultValue)) {
38
40
  throw new Error(
39
41
  `defaultValue ${defaultValue} doesn't pass \`is\` function`,
40
42
  );
@@ -54,7 +56,7 @@ export const brand = <
54
56
  (res): Result<T, readonly ValidationError[]> =>
55
57
  Result.isErr(res)
56
58
  ? res
57
- : is(res.value)
59
+ : isFilled(res.value)
58
60
  ? Result.ok(res.value satisfies T)
59
61
  : Result.err([
60
62
  {
@@ -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,37 @@
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
+ // An absolute URI/URL 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
+ // `URL.canParse` trims leading/trailing ASCII whitespace per the WHATWG URL parser, so
30
+ // surrounding whitespace must be rejected explicitly to avoid accepting e.g. " https://example.com ".
31
+ const isUri = (s: string): s is Uri => s === s.trim() && URL.canParse(s);
32
+
33
+ if (import.meta.vitest !== undefined) {
34
+ test('defaultUri', () => {
35
+ assert.isTrue(isUri(defaultUri));
36
+ });
37
+ }
@@ -0,0 +1,82 @@
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
+ ' https://example.com',
26
+ 'https://example.com ',
27
+ ' https://example.com ',
28
+ ] as const satisfies readonly string[];
29
+
30
+ describe(uri, () => {
31
+ const baseType = uri();
32
+
33
+ type UriType = TypeOf<typeof baseType>;
34
+
35
+ expectType<UriType, string>('<=');
36
+
37
+ expectType<UriType, NonEmptyString>('<=');
38
+
39
+ expectType<typeof baseType.defaultValue, UriType>('=');
40
+
41
+ test('provides https://example.com as default', () => {
42
+ expect(baseType.defaultValue).toBe('https://example.com');
43
+ });
44
+
45
+ test.each(validSamples)('accepts %s', (u) => {
46
+ assert.isTrue(baseType.is(u));
47
+ });
48
+
49
+ test.each(invalidSamples)('rejects %s', (u) => {
50
+ assert.isFalse(baseType.is(u));
51
+ });
52
+
53
+ test('validate surfaces details for invalid strings', () => {
54
+ const result = baseType.validate('not a uri');
55
+
56
+ assert.isTrue(Result.isErr(result));
57
+
58
+ if (!Result.isErr(result)) {
59
+ throw new Error('Expected validation failure');
60
+ }
61
+
62
+ assert.deepStrictEqual(result.value, [
63
+ {
64
+ path: [],
65
+ actualValue: 'not a uri',
66
+ expectedType: 'Uri',
67
+ typeName: 'Uri',
68
+ details: undefined,
69
+ },
70
+ ]);
71
+
72
+ assert.deepStrictEqual(validationErrorsToMessages(result.value), [
73
+ 'Error: expected <Uri> type but <string> type value "not a uri" was passed.',
74
+ ]);
75
+ });
76
+
77
+ test('accepts a custom default value', () => {
78
+ const customType = uri({ defaultValue: 'mailto:foo@bar.com' });
79
+
80
+ expect(customType.defaultValue).toBe('mailto:foo@bar.com');
81
+ });
82
+ });