@othree.io/auditor 2.1.0 → 4.0.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.
package/README.md CHANGED
@@ -31,7 +31,7 @@ const UserSchema = z.object({
31
31
 
32
32
  type User = z.infer<typeof UserSchema>
33
33
 
34
- const validateUser = zodValidate(UserSchema)
34
+ const validateUser = zodValidate({ constraints: UserSchema })
35
35
 
36
36
  const result = validateUser({ name: 'Alice', age: 30 })
37
37
 
@@ -56,10 +56,26 @@ The library exports common string constraints ready to use with `zodValidate`:
56
56
  ```typescript
57
57
  import { zodValidate, NonEmptyString, CountryISOCode, LanguageCode } from '@othree.io/auditor'
58
58
 
59
- zodValidate(NonEmptyString)(' hello ') // Optional('hello')
60
- zodValidate(CountryISOCode)('US') // Optional('US')
61
- zodValidate(LanguageCode)('es') // Optional('es')
62
- zodValidate(LanguageCode)('BAD') // Empty(ValidationError)
59
+ zodValidate({ constraints: NonEmptyString })(' hello ') // Optional('hello')
60
+ zodValidate({ constraints: CountryISOCode })('US') // Optional('US')
61
+ zodValidate({ constraints: LanguageCode })('es') // Optional('es')
62
+ zodValidate({ constraints: LanguageCode })('BAD') // Empty(ValidationError)
63
+ ```
64
+
65
+ ### `toValidationError`
66
+
67
+ Converts a Zod `ZodError` into a `ValidationError` with per-field error messages. Used internally by `zodValidate`, but available if you need to work with Zod errors directly.
68
+
69
+ ```typescript
70
+ import { toValidationError } from '@othree.io/auditor'
71
+ import { z } from 'zod'
72
+
73
+ const result = z.string().min(1).safeParse('')
74
+
75
+ if (!result.success) {
76
+ const error = toValidationError(result.error)
77
+ console.log(error.errors) // { input: ['String must contain at least 1 character(s)'] }
78
+ }
63
79
  ```
64
80
 
65
81
  ### `ValidationError`
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,OAAO,CAAA"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,wCAAqB","sourcesContent":["export * from './validation-error'\nexport * from './zod'\n"]}
@@ -1,4 +1,4 @@
1
- type FieldsValidationErrors = Record<string, Array<string>>;
1
+ type FieldsValidationErrors = Readonly<Record<string, ReadonlyArray<string>>>;
2
2
  export declare class ValidationError extends Error {
3
3
  static readonly ERROR = "ValidationError";
4
4
  readonly errors: FieldsValidationErrors;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation-error.d.ts","sourceRoot":"","sources":["../../src/validation-error.ts"],"names":[],"mappings":"AAAA,KAAK,sBAAsB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AAE7E,qBAAa,eAAgB,SAAQ,KAAK;IACtC,MAAM,CAAC,QAAQ,CAAC,KAAK,qBAAoB;IACzC,QAAQ,CAAC,MAAM,EAAE,sBAAsB,CAAA;gBAE3B,gBAAgB,EAAE,sBAAsB;CAMvD"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation-error.js","sourceRoot":"","sources":["../../src/validation-error.ts"],"names":[],"mappings":";;;AAEA,MAAa,eAAgB,SAAQ,KAAK;IAItC,YAAY,gBAAwC;QAChD,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC,CAAA;QACvC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;QACjD,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC,KAAK,CAAA;QACjC,IAAI,CAAC,MAAM,GAAG,gBAAgB,CAAA;IAClC,CAAC;;AATL,0CAUC;AATmB,qBAAK,GAAG,iBAAiB,CAAA","sourcesContent":["type FieldsValidationErrors = Readonly<Record<string, ReadonlyArray<string>>>\n\nexport class ValidationError extends Error {\n static readonly ERROR = 'ValidationError'\n readonly errors: FieldsValidationErrors\n\n constructor(validationErrors: FieldsValidationErrors) {\n super(JSON.stringify(validationErrors))\n Object.setPrototypeOf(this, new.target.prototype)\n this.name = ValidationError.ERROR\n this.errors = validationErrors\n }\n}\n"]}
@@ -1,9 +1,7 @@
1
1
  import { ZodError, z, ZodType } from 'zod';
2
2
  import { Optional } from '@othree.io/optional';
3
3
  import { ValidationError } from './validation-error';
4
- export declare const toValidationError: (deps: {
5
- ValidationError: new (errors: Record<string, Array<string>>) => ValidationError;
6
- }) => (error: ZodError) => ValidationError;
4
+ export declare const toValidationError: (error: ZodError) => ValidationError;
7
5
  export declare const zodValidate: <T>(deps: {
8
6
  constraints: ZodType<T>;
9
7
  }) => (input: T) => Optional<T>;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zod.d.ts","sourceRoot":"","sources":["../../src/zod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,CAAC,EAAE,OAAO,EAAqB,MAAM,KAAK,CAAA;AAC5D,OAAO,EAAQ,QAAQ,EAAC,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAC,eAAe,EAAC,MAAM,oBAAoB,CAAA;AAElD,eAAO,MAAM,iBAAiB,GAAI,OAAO,QAAQ,oBAahD,CAAA;AAED,eAAO,MAAM,WAAW,GAAI,CAAC,EAAE,MAAM;IACjC,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;CAC1B,MAAM,OAAO,CAAC,KAAG,QAAQ,CAAC,CAAC,CAG3B,CAAA;AAED,eAAO,MAAM,cAAc,aAA2B,CAAA;AACtD,eAAO,MAAM,cAAc,aAAiE,CAAA;AAC5F,eAAO,MAAM,YAAY,aAA8D,CAAA"}
@@ -4,27 +4,27 @@ exports.LanguageCode = exports.CountryISOCode = exports.NonEmptyString = exports
4
4
  const zod_1 = require("zod");
5
5
  const optional_1 = require("@othree.io/optional");
6
6
  const validation_error_1 = require("./validation-error");
7
- const toValidationError = (deps) => (error) => {
7
+ const toValidationError = (error) => {
8
8
  const errors = error.issues.reduce((acc, issue) => {
9
9
  if (issue.code === 'unrecognized_keys') {
10
10
  issue.keys.forEach(key => {
11
- acc[key] = [`Unrecognized key: ${key}`];
11
+ acc[key] = [...(acc[key] ?? []), `Unrecognized key: ${key}`];
12
12
  });
13
13
  return acc;
14
14
  }
15
15
  const path = issue.path.length === 0 ? 'input' : issue.path.join('.');
16
- acc[path] = [issue.message];
16
+ acc[path] = [...(acc[path] ?? []), issue.message];
17
17
  return acc;
18
18
  }, {});
19
- return new deps.ValidationError(errors);
19
+ return new validation_error_1.ValidationError(errors);
20
20
  };
21
21
  exports.toValidationError = toValidationError;
22
22
  const zodValidate = (deps) => (input) => {
23
23
  const maybeInput = deps.constraints.safeParse(input);
24
- return (maybeInput.success) ? (0, optional_1.Optional)(maybeInput.data) : (0, optional_1.Empty)((0, exports.toValidationError)({ ValidationError: validation_error_1.ValidationError })(maybeInput.error));
24
+ return (maybeInput.success) ? (0, optional_1.Optional)(maybeInput.data) : (0, optional_1.Empty)((0, exports.toValidationError)(maybeInput.error));
25
25
  };
26
26
  exports.zodValidate = zodValidate;
27
27
  exports.NonEmptyString = zod_1.z.string().trim().min(1);
28
- exports.CountryISOCode = exports.NonEmptyString.regex(/^[A-Z]{2}/g, 'Invalid country ISO code');
29
- exports.LanguageCode = exports.NonEmptyString.regex(/^[a-z]{2}/g, 'Invalid language code');
28
+ exports.CountryISOCode = exports.NonEmptyString.regex(/^[A-Z]{2}$/, 'Invalid country ISO code');
29
+ exports.LanguageCode = exports.NonEmptyString.regex(/^[a-z]{2}$/, 'Invalid language code');
30
30
  //# sourceMappingURL=zod.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zod.js","sourceRoot":"","sources":["../../src/zod.ts"],"names":[],"mappings":";;;AAAA,6BAA4D;AAC5D,kDAAmD;AACnD,yDAAkD;AAE3C,MAAM,iBAAiB,GAAG,CAAC,KAAe,EAAE,EAAE;IACjD,MAAM,MA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,WAAW,eAKvB;AAEY,QAAA,cAAc,GAAG,OAAC,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;AACzC,QAAA,cAAc,GAAG,sBAAc,CAAC,KAAK,CAAC,YAAY,EAAE,0BAA0B,CAAC,CAAA;AAC/E,QAAA,YAAY,GAAG,sBAAc,CAAC,KAAK,CAAC,YAAY,EAAE,uBAAuB,CAAC,CAAA","sourcesContent":["import {ZodError, z, ZodType, ZodSafeParseResult} from 'zod'\nimport {Empty, Optional} from '@othree.io/optional'\nimport {ValidationError} from './validation-error'\n\nexport const toValidationError = (error: ZodError) => {\n const errors = error.issues.reduce((acc: Record<string, Array<string>>, issue) => {\n if (issue.code === 'unrecognized_keys') {\n issue.keys.forEach(key => {\n acc[key] = [...(acc[key] ?? []), `Unrecognized key: ${key}`]\n })\n return acc\n }\n const path = issue.path.length === 0 ? 'input' : issue.path.join('.')\n acc[path] = [...(acc[path] ?? []), issue.message]\n return acc\n }, {})\n return new ValidationError(errors)\n}\n\nexport const zodValidate = <T>(deps: {\n constraints: ZodType<T>\n}) => (input: T): Optional<T> => {\n const maybeInput: ZodSafeParseResult<T> = deps.constraints.safeParse(input)\n return (maybeInput.success) ? Optional<T>(maybeInput.data) : Empty<T>(toValidationError(maybeInput.error))\n}\n\nexport const NonEmptyString = z.string().trim().min(1)\nexport const CountryISOCode = NonEmptyString.regex(/^[A-Z]{2}$/, 'Invalid country ISO code')\nexport const LanguageCode = NonEmptyString.regex(/^[a-z]{2}$/, 'Invalid language code')\n"]}
@@ -0,0 +1,3 @@
1
+ export * from './validation-error';
2
+ export * from './zod';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,OAAO,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './validation-error';
2
+ export * from './zod';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,OAAO,CAAA","sourcesContent":["export * from './validation-error'\nexport * from './zod'\n"]}
@@ -0,0 +1,8 @@
1
+ type FieldsValidationErrors = Readonly<Record<string, ReadonlyArray<string>>>;
2
+ export declare class ValidationError extends Error {
3
+ static readonly ERROR = "ValidationError";
4
+ readonly errors: FieldsValidationErrors;
5
+ constructor(validationErrors: FieldsValidationErrors);
6
+ }
7
+ export {};
8
+ //# sourceMappingURL=validation-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation-error.d.ts","sourceRoot":"","sources":["../../src/validation-error.ts"],"names":[],"mappings":"AAAA,KAAK,sBAAsB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AAE7E,qBAAa,eAAgB,SAAQ,KAAK;IACtC,MAAM,CAAC,QAAQ,CAAC,KAAK,qBAAoB;IACzC,QAAQ,CAAC,MAAM,EAAE,sBAAsB,CAAA;gBAE3B,gBAAgB,EAAE,sBAAsB;CAMvD"}
@@ -0,0 +1,10 @@
1
+ export class ValidationError extends Error {
2
+ constructor(validationErrors) {
3
+ super(JSON.stringify(validationErrors));
4
+ Object.setPrototypeOf(this, new.target.prototype);
5
+ this.name = ValidationError.ERROR;
6
+ this.errors = validationErrors;
7
+ }
8
+ }
9
+ ValidationError.ERROR = 'ValidationError';
10
+ //# sourceMappingURL=validation-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation-error.js","sourceRoot":"","sources":["../../src/validation-error.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,eAAgB,SAAQ,KAAK;IAItC,YAAY,gBAAwC;QAChD,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC,CAAA;QACvC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;QACjD,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC,KAAK,CAAA;QACjC,IAAI,CAAC,MAAM,GAAG,gBAAgB,CAAA;IAClC,CAAC;;AARe,qBAAK,GAAG,iBAAiB,CAAA","sourcesContent":["type FieldsValidationErrors = Readonly<Record<string, ReadonlyArray<string>>>\n\nexport class ValidationError extends Error {\n static readonly ERROR = 'ValidationError'\n readonly errors: FieldsValidationErrors\n\n constructor(validationErrors: FieldsValidationErrors) {\n super(JSON.stringify(validationErrors))\n Object.setPrototypeOf(this, new.target.prototype)\n this.name = ValidationError.ERROR\n this.errors = validationErrors\n }\n}\n"]}
@@ -0,0 +1,11 @@
1
+ import { ZodError, z, ZodType } from 'zod';
2
+ import { Optional } from '@othree.io/optional';
3
+ import { ValidationError } from './validation-error';
4
+ export declare const toValidationError: (error: ZodError) => ValidationError;
5
+ export declare const zodValidate: <T>(deps: {
6
+ constraints: ZodType<T>;
7
+ }) => (input: T) => Optional<T>;
8
+ export declare const NonEmptyString: z.ZodString;
9
+ export declare const CountryISOCode: z.ZodString;
10
+ export declare const LanguageCode: z.ZodString;
11
+ //# sourceMappingURL=zod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zod.d.ts","sourceRoot":"","sources":["../../src/zod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,CAAC,EAAE,OAAO,EAAqB,MAAM,KAAK,CAAA;AAC5D,OAAO,EAAQ,QAAQ,EAAC,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAC,eAAe,EAAC,MAAM,oBAAoB,CAAA;AAElD,eAAO,MAAM,iBAAiB,GAAI,OAAO,QAAQ,oBAahD,CAAA;AAED,eAAO,MAAM,WAAW,GAAI,CAAC,EAAE,MAAM;IACjC,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;CAC1B,MAAM,OAAO,CAAC,KAAG,QAAQ,CAAC,CAAC,CAG3B,CAAA;AAED,eAAO,MAAM,cAAc,aAA2B,CAAA;AACtD,eAAO,MAAM,cAAc,aAAiE,CAAA;AAC5F,eAAO,MAAM,YAAY,aAA8D,CAAA"}
package/lib/esm/zod.js ADDED
@@ -0,0 +1,25 @@
1
+ import { z } from 'zod';
2
+ import { Empty, Optional } from '@othree.io/optional';
3
+ import { ValidationError } from './validation-error';
4
+ export const toValidationError = (error) => {
5
+ const errors = error.issues.reduce((acc, issue) => {
6
+ if (issue.code === 'unrecognized_keys') {
7
+ issue.keys.forEach(key => {
8
+ acc[key] = [...(acc[key] ?? []), `Unrecognized key: ${key}`];
9
+ });
10
+ return acc;
11
+ }
12
+ const path = issue.path.length === 0 ? 'input' : issue.path.join('.');
13
+ acc[path] = [...(acc[path] ?? []), issue.message];
14
+ return acc;
15
+ }, {});
16
+ return new ValidationError(errors);
17
+ };
18
+ export const zodValidate = (deps) => (input) => {
19
+ const maybeInput = deps.constraints.safeParse(input);
20
+ return (maybeInput.success) ? Optional(maybeInput.data) : Empty(toValidationError(maybeInput.error));
21
+ };
22
+ export const NonEmptyString = z.string().trim().min(1);
23
+ export const CountryISOCode = NonEmptyString.regex(/^[A-Z]{2}$/, 'Invalid country ISO code');
24
+ export const LanguageCode = NonEmptyString.regex(/^[a-z]{2}$/, 'Invalid language code');
25
+ //# sourceMappingURL=zod.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zod.js","sourceRoot":"","sources":["../../src/zod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,CAAC,EAA8B,MAAM,KAAK,CAAA;AAC5D,OAAO,EAAC,KAAK,EAAE,QAAQ,EAAC,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAC,eAAe,EAAC,MAAM,oBAAoB,CAAA;AAElD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,KAAe,EAAE,EAAE;IACjD,MAAM,MAAM,GAAG,KAAK,CAAC,MAA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E,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;AACtD,MAAM,CAAC,MAAM,cAAc,GAAG,cAAc,CAAC,KAAK,CAAC,YAAY,EAAE,0BAA0B,CAAC,CAAA;AAC5F,MAAM,CAAC,MAAM,YAAY,GAAG,cAAc,CAAC,KAAK,CAAC,YAAY,EAAE,uBAAuB,CAAC,CAAA","sourcesContent":["import {ZodError, z, ZodType, ZodSafeParseResult} from 'zod'\nimport {Empty, Optional} from '@othree.io/optional'\nimport {ValidationError} from './validation-error'\n\nexport const toValidationError = (error: ZodError) => {\n const errors = error.issues.reduce((acc: Record<string, Array<string>>, issue) => {\n if (issue.code === 'unrecognized_keys') {\n issue.keys.forEach(key => {\n acc[key] = [...(acc[key] ?? []), `Unrecognized key: ${key}`]\n })\n return acc\n }\n const path = issue.path.length === 0 ? 'input' : issue.path.join('.')\n acc[path] = [...(acc[path] ?? []), issue.message]\n return acc\n }, {})\n return new ValidationError(errors)\n}\n\nexport const zodValidate = <T>(deps: {\n constraints: ZodType<T>\n}) => (input: T): Optional<T> => {\n const maybeInput: ZodSafeParseResult<T> = deps.constraints.safeParse(input)\n return (maybeInput.success) ? Optional<T>(maybeInput.data) : Empty<T>(toValidationError(maybeInput.error))\n}\n\nexport const NonEmptyString = z.string().trim().min(1)\nexport const CountryISOCode = NonEmptyString.regex(/^[A-Z]{2}$/, 'Invalid country ISO code')\nexport const LanguageCode = NonEmptyString.regex(/^[a-z]{2}$/, 'Invalid language code')\n"]}
package/package.json CHANGED
@@ -1,16 +1,41 @@
1
1
  {
2
2
  "name": "@othree.io/auditor",
3
- "version": "2.1.0",
4
- "description": "Auditor",
5
- "main": "lib/index.js",
3
+ "version": "4.0.0",
4
+ "description": "A functional validation library built on Zod that returns Optional<T> instead of throwing exceptions",
5
+ "main": "lib/cjs/index.js",
6
+ "module": "lib/esm/index.js",
7
+ "types": "lib/cjs/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./lib/esm/index.d.ts",
12
+ "default": "./lib/esm/index.js"
13
+ },
14
+ "require": {
15
+ "types": "./lib/cjs/index.d.ts",
16
+ "default": "./lib/cjs/index.js"
17
+ }
18
+ }
19
+ },
20
+ "files": [
21
+ "lib"
22
+ ],
6
23
  "scripts": {
7
24
  "test": "vitest run",
8
- "build": "tsc",
25
+ "build": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json",
9
26
  "semantic-release": "semantic-release"
10
27
  },
11
28
  "keywords": [
12
- "auditor"
29
+ "validation",
30
+ "zod",
31
+ "functional",
32
+ "optional",
33
+ "typescript"
13
34
  ],
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://gitlab.com/othree.oss/auditor.git"
38
+ },
14
39
  "author": "othree",
15
40
  "license": "ISC",
16
41
  "peerDependencies": {
package/.gitlab-ci.yml DELETED
@@ -1,29 +0,0 @@
1
- image: node:22-alpine
2
-
3
- cache:
4
- key: ${CI_PIPELINE_ID}
5
- paths:
6
- - node_modules/
7
- - lib/
8
-
9
- build:
10
- stage: build
11
- script:
12
- - npm install && npm run build
13
-
14
- test:
15
- stage: test
16
- script:
17
- - npm test
18
-
19
- deploy:
20
- image: node:22
21
- stage: deploy
22
- variables:
23
- NPM_CONFIG_USERCONFIG: '$CI_PROJECT_DIR/.npmrc'
24
- script:
25
- - cat $NPM > .npmrc
26
- - npx semantic-release
27
- only:
28
- refs:
29
- - main
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,OAAO,CAAA"}
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,wCAAqB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"validation-error.d.ts","sourceRoot":"","sources":["../src/validation-error.ts"],"names":[],"mappings":"AAAA,KAAK,sBAAsB,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAA;AAE3D,qBAAa,eAAgB,SAAQ,KAAK;IACtC,MAAM,CAAC,QAAQ,CAAC,KAAK,qBAAoB;IACzC,QAAQ,CAAC,MAAM,EAAE,sBAAsB,CAAA;gBAE3B,gBAAgB,EAAE,sBAAsB;CAMvD"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"validation-error.js","sourceRoot":"","sources":["../src/validation-error.ts"],"names":[],"mappings":";;;AAEA,MAAa,eAAgB,SAAQ,KAAK;IAItC,YAAY,gBAAwC;QAChD,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC,CAAA;QACvC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;QACjD,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC,KAAK,CAAA;QACjC,IAAI,CAAC,MAAM,GAAG,gBAAgB,CAAA;IAClC,CAAC;;AATL,0CAUC;AATmB,qBAAK,GAAG,iBAAiB,CAAA"}
package/lib/zod.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"zod.d.ts","sourceRoot":"","sources":["../src/zod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,CAAC,EAAE,OAAO,EAAqB,MAAM,KAAK,CAAA;AAC5D,OAAO,EAAQ,QAAQ,EAAC,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAC,eAAe,EAAC,MAAM,oBAAoB,CAAA;AAElD,eAAO,MAAM,iBAAiB,GAAI,MAAM;IACpC,eAAe,EAAE,KAAK,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,eAAe,CAAA;CAClF,MAAM,OAAO,QAAQ,oBAarB,CAAA;AAED,eAAO,MAAM,WAAW,GAAI,CAAC,EAAE,MAAM;IACjC,WAAW,E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AE,EAAE,CAAC,CAAC,KAAQ,EAAe,EAAE;IAC5B,MAAM,UAAU,GAA0B,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;IAC3E,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAA,mBAAQ,EAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAA,gBAAK,EAAI,IAAA,yBAAiB,EAAC,EAAC,eAAe,EAAf,kCAAe,EAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;AACjI,CAAC,CAAA;AALY,QAAA,WAAW,eAKvB;AAEY,QAAA,cAAc,GAAG,OAAC,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;AACzC,QAAA,cAAc,GAAG,sBAAc,CAAC,KAAK,CAAC,YAAY,EAAE,0BAA0B,CAAC,CAAA;AAC/E,QAAA,YAAY,GAAG,sBAAc,CAAC,KAAK,CAAC,YAAY,EAAE,uBAAuB,CAAC,CAAA"}
package/release.config.js DELETED
@@ -1,4 +0,0 @@
1
- module.exports = {
2
- plugins: ['@semantic-release/commit-analyzer', '@semantic-release/release-notes-generator', '@semantic-release/npm'],
3
- branches: ['master', 'main']
4
- }
package/vitest.config.mts DELETED
@@ -1,20 +0,0 @@
1
- import {defineConfig} from 'vitest/config'
2
-
3
- export default defineConfig({
4
- test: {
5
- coverage: {
6
- include: ['src/**/*'],
7
- enabled: true,
8
- provider: 'istanbul',
9
- all: true,
10
- thresholds: {
11
- branches: 100,
12
- functions: 100,
13
- lines: 100,
14
- statements: 100,
15
- },
16
- reporter: ['text','html'],
17
- reportOnFailure: true
18
- },
19
- },
20
- })
File without changes
File without changes