@domain-first/types 3.0.0 → 3.0.1

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/dist/index.cjs CHANGED
@@ -1,129 +1,2 @@
1
- "use strict";
2
- var __webpack_require__ = {};
3
- (()=>{
4
- __webpack_require__.d = (exports1, getters, values)=>{
5
- var define = (defs, kind)=>{
6
- for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
7
- enumerable: true,
8
- [kind]: defs[key]
9
- });
10
- };
11
- define(getters, "get");
12
- define(values, "value");
13
- };
14
- })();
15
- (()=>{
16
- __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
17
- })();
18
- (()=>{
19
- __webpack_require__.r = (exports1)=>{
20
- if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
21
- value: 'Module'
22
- });
23
- Object.defineProperty(exports1, '__esModule', {
24
- value: true
25
- });
26
- };
27
- })();
28
- var __webpack_exports__ = {};
29
- __webpack_require__.r(__webpack_exports__);
30
- __webpack_require__.d(__webpack_exports__, {
31
- AsyncSchemaInSyncParsingError: ()=>AsyncSchemaInSyncParsingError,
32
- InvalidDataParsingError: ()=>InvalidDataParsingError,
33
- defineEntity: ()=>defineEntity,
34
- defineRecursiveEntity: ()=>defineRecursiveEntity,
35
- defineRecursiveValueObject: ()=>defineRecursiveValueObject,
36
- defineValueObject: ()=>defineValueObject
37
- });
38
- const errors_namespaceObject = require("@domain-first/errors");
39
- const AsyncSchemaInSyncParsingError = (0, errors_namespaceObject.defineErrorClass)({
40
- code: 'ASYNC_SCHEMA_IN_SYNC_PARSING'
41
- });
42
- const InvalidDataParsingError = (0, errors_namespaceObject.defineErrorClass)({
43
- code: 'INVALID_DATA_PARSING'
44
- });
45
- function parseSync(schema, value) {
46
- const result = schema['~standard'].validate(value);
47
- if (result instanceof Promise) throw new AsyncSchemaInSyncParsingError({
48
- schema,
49
- value
50
- });
51
- if ('issues' in result && result.issues) throw new InvalidDataParsingError({
52
- parsingIssues: result.issues,
53
- value
54
- });
55
- return result.value;
56
- }
57
- const createEntity = (idSchema, getModelSchema)=>{
58
- const idSymbol = Symbol();
59
- const modelSymbol = Symbol();
60
- const classSymbol = Symbol();
61
- const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
62
- class Entity {
63
- static idSchema = idSchema;
64
- static modelSchema = getModelSchema(isThisClass);
65
- constructor(id, model){
66
- const parsedId = parseSync(Entity.idSchema, id);
67
- const parsedModelData = parseSync(Entity.modelSchema, model);
68
- Object.defineProperty(this, idSymbol, {
69
- value: parsedId
70
- });
71
- Object.defineProperty(this, modelSymbol, {
72
- value: parsedModelData
73
- });
74
- Object.defineProperty(this, classSymbol, {
75
- value: true
76
- });
77
- }
78
- get model() {
79
- return this[modelSymbol];
80
- }
81
- get id() {
82
- return this[idSymbol];
83
- }
84
- }
85
- return Entity;
86
- };
87
- const defineEntity = (idSchema, modelSchema)=>createEntity(idSchema, ()=>modelSchema);
88
- const defineRecursiveEntity = (idSchema, modelSchema)=>createEntity(idSchema, modelSchema);
89
- const createValueObject = (getSchema)=>{
90
- const modelSymbol = Symbol();
91
- const classSymbol = Symbol();
92
- const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
93
- class ValueObject {
94
- static schema = getSchema(isThisClass);
95
- constructor(data){
96
- const parsedData = parseSync(ValueObject.schema, data);
97
- Object.defineProperty(this, modelSymbol, {
98
- value: parsedData
99
- });
100
- Object.defineProperty(this, classSymbol, {
101
- value: true
102
- });
103
- }
104
- get model() {
105
- const thisWithSymbol = this;
106
- return thisWithSymbol[modelSymbol];
107
- }
108
- }
109
- return ValueObject;
110
- };
111
- const defineValueObject = (schema)=>createValueObject(()=>schema);
112
- const defineRecursiveValueObject = (schema)=>createValueObject(schema);
113
- exports.AsyncSchemaInSyncParsingError = __webpack_exports__.AsyncSchemaInSyncParsingError;
114
- exports.InvalidDataParsingError = __webpack_exports__.InvalidDataParsingError;
115
- exports.defineEntity = __webpack_exports__.defineEntity;
116
- exports.defineRecursiveEntity = __webpack_exports__.defineRecursiveEntity;
117
- exports.defineRecursiveValueObject = __webpack_exports__.defineRecursiveValueObject;
118
- exports.defineValueObject = __webpack_exports__.defineValueObject;
119
- for(var __rspack_i in __webpack_exports__)if (-1 === [
120
- "AsyncSchemaInSyncParsingError",
121
- "InvalidDataParsingError",
122
- "defineEntity",
123
- "defineRecursiveEntity",
124
- "defineRecursiveValueObject",
125
- "defineValueObject"
126
- ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
127
- Object.defineProperty(exports, '__esModule', {
128
- value: true
129
- });
1
+ "use strict";const __rslib_import_meta_url__="u"<typeof document?new(require("url".replace("",""))).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href;var __webpack_require__={};__webpack_require__.d=(e,r,t)=>{var a=(r,t)=>{for(var a in r)__webpack_require__.o(r,a)&&!__webpack_require__.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,[t]:r[a]})};a(r,"get"),a(t,"value")},__webpack_require__.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),__webpack_require__.r=e=>{"u">typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var __webpack_exports__={};__webpack_require__.r(__webpack_exports__),__webpack_require__.d(__webpack_exports__,{AsyncSchemaInSyncParsingError:()=>AsyncSchemaInSyncParsingError,InvalidDataParsingError:()=>InvalidDataParsingError,defineEntity:()=>defineEntity,defineRecursiveEntity:()=>defineRecursiveEntity,defineRecursiveValueObject:()=>defineRecursiveValueObject,defineValueObject:()=>defineValueObject});const errors_namespaceObject=require("@domain-first/errors"),AsyncSchemaInSyncParsingError=(0,errors_namespaceObject.defineErrorClass)({code:"ASYNC_SCHEMA_IN_SYNC_PARSING"}),InvalidDataParsingError=(0,errors_namespaceObject.defineErrorClass)({code:"INVALID_DATA_PARSING"});function parseSync(e,r){let t=e["~standard"].validate(r);if(t instanceof Promise)throw new AsyncSchemaInSyncParsingError({schema:e,value:r});if("issues"in t&&t.issues)throw new InvalidDataParsingError({parsingIssues:t.issues,value:r});return t.value}const createEntity=(e,r)=>{let t=Symbol(),a=Symbol(),n=Symbol(),i=e=>!!e&&"object"==typeof e&&n in e;class c{static idSchema=e;static modelSchema=r(i);constructor(e,r){const i=parseSync(c.idSchema,e),_=parseSync(c.modelSchema,r);Object.defineProperty(this,t,{value:i}),Object.defineProperty(this,a,{value:_}),Object.defineProperty(this,n,{value:!0})}get model(){return this[a]}get id(){return this[t]}}return c},defineEntity=(e,r)=>createEntity(e,()=>r),defineRecursiveEntity=(e,r)=>createEntity(e,r),createValueObject=e=>{let r=Symbol(),t=Symbol(),a=e=>!!e&&"object"==typeof e&&t in e;class n{static schema=e(a);constructor(e){Object.defineProperty(this,r,{value:parseSync(n.schema,e)}),Object.defineProperty(this,t,{value:!0})}get model(){return this[r]}}return n},defineValueObject=e=>createValueObject(()=>e),defineRecursiveValueObject=e=>createValueObject(e);for(var __rspack_i in exports.AsyncSchemaInSyncParsingError=__webpack_exports__.AsyncSchemaInSyncParsingError,exports.InvalidDataParsingError=__webpack_exports__.InvalidDataParsingError,exports.defineEntity=__webpack_exports__.defineEntity,exports.defineRecursiveEntity=__webpack_exports__.defineRecursiveEntity,exports.defineRecursiveValueObject=__webpack_exports__.defineRecursiveValueObject,exports.defineValueObject=__webpack_exports__.defineValueObject,__webpack_exports__)-1===["AsyncSchemaInSyncParsingError","InvalidDataParsingError","defineEntity","defineRecursiveEntity","defineRecursiveValueObject","defineValueObject"].indexOf(__rspack_i)&&(exports[__rspack_i]=__webpack_exports__[__rspack_i]);Object.defineProperty(exports,"__esModule",{value:!0});
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["webpack://webpack/runtime/define_property_getters","webpack://webpack/runtime/has_own_property","webpack://webpack/runtime/make_namespace_object","../src/errors/async-schema-in-sync-parsing-error.ts","../src/errors/invalid-data-parsing-error.ts","../src/utils/parse-sync.ts","../src/entity.ts","../src/value-object.ts"],"sourcesContent":["__webpack_require__.d = (exports, getters, values) => {\n\tvar define = (defs, kind) => {\n\t\tfor(var key in defs) {\n\t\t\tif(__webpack_require__.o(defs, key) && !__webpack_require__.o(exports, key)) {\n\t\t\t\tObject.defineProperty(exports, key, { enumerable: true, [kind]: defs[key] });\n\t\t\t}\n\t\t}\n\t};\n\tdefine(getters, \"get\");\n\tdefine(values, \"value\");\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","import { defineErrorClass } from '@domain-first/errors';\n\nexport const AsyncSchemaInSyncParsingError = defineErrorClass({\n code: 'ASYNC_SCHEMA_IN_SYNC_PARSING'\n});\n","import { defineErrorClass } from '@domain-first/errors';\nimport type { StandardSchemaV1 } from '@standard-schema/spec';\n\nexport const InvalidDataParsingError = defineErrorClass<{\n parsingIssues: readonly StandardSchemaV1.Issue[];\n value: unknown;\n}>({\n code: 'INVALID_DATA_PARSING'\n});\n","import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport {\n AsyncSchemaInSyncParsingError,\n InvalidDataParsingError\n} from '../errors';\n\nexport function parseSync<S extends StandardSchemaV1>(\n schema: S,\n value: StandardSchemaV1.InferInput<S>\n): StandardSchemaV1.InferOutput<S> {\n const result = schema['~standard'].validate(value);\n\n if (result instanceof Promise) {\n throw new AsyncSchemaInSyncParsingError({ schema, value });\n }\n\n if ('issues' in result && result.issues) {\n throw new InvalidDataParsingError({ parsingIssues: result.issues, value });\n }\n\n return result.value;\n}\n","import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport { type DeepReadonly, parseSync } from './utils';\n\nconst createEntity = <\n IdSchema extends StandardSchemaV1,\n ModelSchema extends StandardSchemaV1\n>(\n idSchema: IdSchema,\n getModelSchema: (\n isCurrentEntity: (target: unknown) => boolean\n ) => ModelSchema\n) => {\n const idSymbol = Symbol();\n const modelSymbol = Symbol();\n const classSymbol = Symbol();\n\n const isThisClass = (target: unknown): boolean => {\n return !!target && typeof target === 'object' && classSymbol in target;\n };\n\n abstract class Entity {\n public static readonly idSchema = idSchema;\n public static readonly modelSchema = getModelSchema(isThisClass);\n\n constructor(\n id: StandardSchemaV1.InferInput<IdSchema>,\n model: StandardSchemaV1.InferInput<ModelSchema>\n ) {\n const parsedId = parseSync(Entity.idSchema, id);\n const parsedModelData = parseSync(Entity.modelSchema, model);\n\n Object.defineProperty(this, idSymbol, {\n value: parsedId\n });\n\n Object.defineProperty(this, modelSymbol, {\n value: parsedModelData\n });\n\n Object.defineProperty(this, classSymbol, {\n value: true\n });\n }\n\n get model() {\n return (this as any)[modelSymbol] as DeepReadonly<\n StandardSchemaV1.InferOutput<ModelSchema>\n >;\n }\n\n get id() {\n return (this as any)[idSymbol] as DeepReadonly<\n StandardSchemaV1.InferOutput<IdSchema>\n >;\n }\n }\n\n return Entity;\n};\n\nexport const defineEntity = <\n IdSchema extends StandardSchemaV1,\n ModelSchema extends StandardSchemaV1\n>(\n idSchema: IdSchema,\n modelSchema: ModelSchema\n) => {\n return createEntity(idSchema, () => modelSchema);\n};\n\nexport const defineRecursiveEntity = <\n IdSchema extends StandardSchemaV1,\n ModelSchema extends StandardSchemaV1\n>(\n idSchema: IdSchema,\n modelSchema: (isCurrentEntity: (target: unknown) => boolean) => ModelSchema\n) => {\n return createEntity(idSchema, modelSchema);\n};\n\nexport type InferEntityModel<T> = T extends { modelSchema: StandardSchemaV1 }\n ? StandardSchemaV1.InferOutput<T['modelSchema']>\n : T extends { model: infer M }\n ? M\n : never;\n\nexport type InferEntityId<T> = T extends { idSchema: StandardSchemaV1 }\n ? StandardSchemaV1.InferOutput<T['idSchema']>\n : T extends { id: infer M }\n ? M\n : never;\n","import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport { type DeepReadonly, parseSync } from './utils';\n\nconst createValueObject = <Schema extends StandardSchemaV1>(\n getSchema: (isCurrentValueObject: (target: unknown) => boolean) => Schema\n) => {\n const modelSymbol = Symbol();\n const classSymbol = Symbol();\n\n const isThisClass = (target: unknown): boolean => {\n return !!target && typeof target === 'object' && classSymbol in target;\n };\n\n abstract class ValueObject {\n public static readonly schema = getSchema(isThisClass);\n\n constructor(data: StandardSchemaV1.InferInput<Schema>) {\n const parsedData = parseSync(ValueObject.schema, data);\n\n Object.defineProperty(this, modelSymbol, {\n value: parsedData\n });\n\n Object.defineProperty(this, classSymbol, {\n value: true\n });\n }\n\n get model() {\n const thisWithSymbol = this as unknown as {\n [modelSymbol]: StandardSchemaV1.InferOutput<Schema>;\n };\n\n return thisWithSymbol[modelSymbol] as DeepReadonly<\n StandardSchemaV1.InferOutput<Schema>\n >;\n }\n }\n\n return ValueObject;\n};\n\nexport const defineValueObject = <Schema extends StandardSchemaV1>(\n schema: Schema\n) => {\n return createValueObject(() => schema);\n};\n\nexport const defineRecursiveValueObject = <Schema extends StandardSchemaV1>(\n schema: (isCurrentValueObject: (target: unknown) => boolean) => Schema\n) => {\n return createValueObject(schema);\n};\n\nexport type InferValueObjectSchema<T> = T extends {\n schema: infer S extends StandardSchemaV1;\n}\n ? StandardSchemaV1.InferOutput<S>\n : T extends { model: infer M }\n ? M\n : never;\n"],"names":["__webpack_require__","e","Object","Symbol","AsyncSchemaInSyncParsingError","defineErrorClass","InvalidDataParsingError","parseSync","schema","value","result","Promise","createEntity","idSchema","getModelSchema","idSymbol","modelSymbol","classSymbol","isThisClass","target","Entity","id","model","parsedId","parsedModelData","defineEntity","modelSchema","defineRecursiveEntity","createValueObject","getSchema","ValueObject","data","thisWithSymbol","defineValueObject","defineRec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IAAMpB,GAGtB0B,2BAA6B,AACtC1B,GAEOoB,kBAAkBpB,G"}
package/dist/index.js CHANGED
@@ -1,76 +1,2 @@
1
- import { defineErrorClass } from "@domain-first/errors";
2
- const AsyncSchemaInSyncParsingError = defineErrorClass({
3
- code: 'ASYNC_SCHEMA_IN_SYNC_PARSING'
4
- });
5
- const InvalidDataParsingError = defineErrorClass({
6
- code: 'INVALID_DATA_PARSING'
7
- });
8
- function parseSync(schema, value) {
9
- const result = schema['~standard'].validate(value);
10
- if (result instanceof Promise) throw new AsyncSchemaInSyncParsingError({
11
- schema,
12
- value
13
- });
14
- if ('issues' in result && result.issues) throw new InvalidDataParsingError({
15
- parsingIssues: result.issues,
16
- value
17
- });
18
- return result.value;
19
- }
20
- const createEntity = (idSchema, getModelSchema)=>{
21
- const idSymbol = Symbol();
22
- const modelSymbol = Symbol();
23
- const classSymbol = Symbol();
24
- const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
25
- class Entity {
26
- static idSchema = idSchema;
27
- static modelSchema = getModelSchema(isThisClass);
28
- constructor(id, model){
29
- const parsedId = parseSync(Entity.idSchema, id);
30
- const parsedModelData = parseSync(Entity.modelSchema, model);
31
- Object.defineProperty(this, idSymbol, {
32
- value: parsedId
33
- });
34
- Object.defineProperty(this, modelSymbol, {
35
- value: parsedModelData
36
- });
37
- Object.defineProperty(this, classSymbol, {
38
- value: true
39
- });
40
- }
41
- get model() {
42
- return this[modelSymbol];
43
- }
44
- get id() {
45
- return this[idSymbol];
46
- }
47
- }
48
- return Entity;
49
- };
50
- const defineEntity = (idSchema, modelSchema)=>createEntity(idSchema, ()=>modelSchema);
51
- const defineRecursiveEntity = (idSchema, modelSchema)=>createEntity(idSchema, modelSchema);
52
- const createValueObject = (getSchema)=>{
53
- const modelSymbol = Symbol();
54
- const classSymbol = Symbol();
55
- const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
56
- class ValueObject {
57
- static schema = getSchema(isThisClass);
58
- constructor(data){
59
- const parsedData = parseSync(ValueObject.schema, data);
60
- Object.defineProperty(this, modelSymbol, {
61
- value: parsedData
62
- });
63
- Object.defineProperty(this, classSymbol, {
64
- value: true
65
- });
66
- }
67
- get model() {
68
- const thisWithSymbol = this;
69
- return thisWithSymbol[modelSymbol];
70
- }
71
- }
72
- return ValueObject;
73
- };
74
- const defineValueObject = (schema)=>createValueObject(()=>schema);
75
- const defineRecursiveValueObject = (schema)=>createValueObject(schema);
76
- export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, defineEntity, defineRecursiveEntity, defineRecursiveValueObject, defineValueObject };
1
+ import{defineErrorClass as e}from"@domain-first/errors";let t=e({code:"ASYNC_SCHEMA_IN_SYNC_PARSING"}),r=e({code:"INVALID_DATA_PARSING"});function i(e,i){let s=e["~standard"].validate(i);if(s instanceof Promise)throw new t({schema:e,value:i});if("issues"in s&&s.issues)throw new r({parsingIssues:s.issues,value:i});return s.value}let s=(e,t)=>{let r=Symbol(),s=Symbol(),n=Symbol(),o=e=>!!e&&"object"==typeof e&&n in e;class c{static idSchema=e;static modelSchema=t(o);constructor(e,t){let o=i(c.idSchema,e),a=i(c.modelSchema,t);Object.defineProperty(this,r,{value:o}),Object.defineProperty(this,s,{value:a}),Object.defineProperty(this,n,{value:!0})}get model(){return this[s]}get id(){return this[r]}}return c},n=(e,t)=>s(e,()=>t),o=(e,t)=>s(e,t),c=e=>{let t=Symbol(),r=Symbol(),s=e=>!!e&&"object"==typeof e&&r in e;class n{static schema=e(s);constructor(e){Object.defineProperty(this,t,{value:i(n.schema,e)}),Object.defineProperty(this,r,{value:!0})}get model(){return this[t]}}return n},a=e=>c(()=>e),l=e=>c(e);export{t as AsyncSchemaInSyncParsingError,r as InvalidDataParsingError,n as defineEntity,o as defineRecursiveEntity,l as defineRecursiveValueObject,a as defineValueObject};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/errors/async-schema-in-sync-parsing-error.ts","../src/errors/invalid-data-parsing-error.ts","../src/utils/parse-sync.ts","../src/entity.ts","../src/value-object.ts"],"sourcesContent":["import { defineErrorClass } from '@domain-first/errors';\n\nexport const AsyncSchemaInSyncParsingError = defineErrorClass({\n code: 'ASYNC_SCHEMA_IN_SYNC_PARSING'\n});\n","import { defineErrorClass } from '@domain-first/errors';\nimport type { StandardSchemaV1 } from '@standard-schema/spec';\n\nexport const InvalidDataParsingError = defineErrorClass<{\n parsingIssues: readonly StandardSchemaV1.Issue[];\n value: unknown;\n}>({\n code: 'INVALID_DATA_PARSING'\n});\n","import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport {\n AsyncSchemaInSyncParsingError,\n InvalidDataParsingError\n} from '../errors';\n\nexport function parseSync<S extends StandardSchemaV1>(\n schema: S,\n value: StandardSchemaV1.InferInput<S>\n): StandardSchemaV1.InferOutput<S> {\n const result = schema['~standard'].validate(value);\n\n if (result instanceof Promise) {\n throw new AsyncSchemaInSyncParsingError({ schema, value });\n }\n\n if ('issues' in result && result.issues) {\n throw new InvalidDataParsingError({ parsingIssues: result.issues, value });\n }\n\n return result.value;\n}\n","import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport { type DeepReadonly, parseSync } from './utils';\n\nconst createEntity = <\n IdSchema extends StandardSchemaV1,\n ModelSchema extends StandardSchemaV1\n>(\n idSchema: IdSchema,\n getModelSchema: (\n isCurrentEntity: (target: unknown) => boolean\n ) => ModelSchema\n) => {\n const idSymbol = Symbol();\n const modelSymbol = Symbol();\n const classSymbol = Symbol();\n\n const isThisClass = (target: unknown): boolean => {\n return !!target && typeof target === 'object' && classSymbol in target;\n };\n\n abstract class Entity {\n public static readonly idSchema = idSchema;\n public static readonly modelSchema = getModelSchema(isThisClass);\n\n constructor(\n id: StandardSchemaV1.InferInput<IdSchema>,\n model: StandardSchemaV1.InferInput<ModelSchema>\n ) {\n const parsedId = parseSync(Entity.idSchema, id);\n const parsedModelData = parseSync(Entity.modelSchema, model);\n\n Object.defineProperty(this, idSymbol, {\n value: parsedId\n });\n\n Object.defineProperty(this, modelSymbol, {\n value: parsedModelData\n });\n\n Object.defineProperty(this, classSymbol, {\n value: true\n });\n }\n\n get model() {\n return (this as any)[modelSymbol] as DeepReadonly<\n StandardSchemaV1.InferOutput<ModelSchema>\n >;\n }\n\n get id() {\n return (this as any)[idSymbol] as DeepReadonly<\n StandardSchemaV1.InferOutput<IdSchema>\n >;\n }\n }\n\n return Entity;\n};\n\nexport const defineEntity = <\n IdSchema extends StandardSchemaV1,\n ModelSchema extends StandardSchemaV1\n>(\n idSchema: IdSchema,\n modelSchema: ModelSchema\n) => {\n return createEntity(idSchema, () => modelSchema);\n};\n\nexport const defineRecursiveEntity = <\n IdSchema extends StandardSchemaV1,\n ModelSchema extends StandardSchemaV1\n>(\n idSchema: IdSchema,\n modelSchema: (isCurrentEntity: (target: unknown) => boolean) => ModelSchema\n) => {\n return createEntity(idSchema, modelSchema);\n};\n\nexport type InferEntityModel<T> = T extends { modelSchema: StandardSchemaV1 }\n ? StandardSchemaV1.InferOutput<T['modelSchema']>\n : T extends { model: infer M }\n ? M\n : never;\n\nexport type InferEntityId<T> = T extends { idSchema: StandardSchemaV1 }\n ? StandardSchemaV1.InferOutput<T['idSchema']>\n : T extends { id: infer M }\n ? M\n : never;\n","import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport { type DeepReadonly, parseSync } from './utils';\n\nconst createValueObject = <Schema extends StandardSchemaV1>(\n getSchema: (isCurrentValueObject: (target: unknown) => boolean) => Schema\n) => {\n const modelSymbol = Symbol();\n const classSymbol = Symbol();\n\n const isThisClass = (target: unknown): boolean => {\n return !!target && typeof target === 'object' && classSymbol in target;\n };\n\n abstract class ValueObject {\n public static readonly schema = getSchema(isThisClass);\n\n constructor(data: StandardSchemaV1.InferInput<Schema>) {\n const parsedData = parseSync(ValueObject.schema, data);\n\n Object.defineProperty(this, modelSymbol, {\n value: parsedData\n });\n\n Object.defineProperty(this, classSymbol, {\n value: true\n });\n }\n\n get model() {\n const thisWithSymbol = this as unknown as {\n [modelSymbol]: StandardSchemaV1.InferOutput<Schema>;\n };\n\n return thisWithSymbol[modelSymbol] as DeepReadonly<\n StandardSchemaV1.InferOutput<Schema>\n >;\n }\n }\n\n return ValueObject;\n};\n\nexport const defineValueObject = <Schema extends StandardSchemaV1>(\n schema: Schema\n) => {\n return createValueObject(() => schema);\n};\n\nexport const defineRecursiveValueObject = <Schema extends StandardSchemaV1>(\n schema: (isCurrentValueObject: (target: unknown) => boolean) => Schema\n) => {\n return createValueObject(schema);\n};\n\nexport type InferValueObjectSchema<T> = T extends {\n schema: infer S extends StandardSchemaV1;\n}\n ? StandardSchemaV1.InferOutput<S>\n : T extends { model: infer M }\n ? M\n : never;\n"],"names":["AsyncSchemaInSyncParsingError","defineErrorClass","InvalidDataParsingError","parseSync","schema","value","result","Promise","createEntity","idSchema","getModelSchema","idSymbol","Symbol","modelSymbol","classSymbol","isThisClass","target","Entity","id","model","parsedId","parsedModelData","Object","defineEntity","modelSchema","defineRecursiveEntity","createValueObject","getSchema","ValueObject","data","thisWithSymbol","defineValueObject","defineRecursiveValueObject"],"mappings":"wDAEO,IAAMA,EAAgCC,EAAiB,CAC1D,KAAM,8BACV,GCDaC,EAA0BD,EAGpC,CACC,KAAM,sBACV,GCFO,SAASE,EACZC,CAAS,CACTC,CAAqC,EAErC,IAAMC,EAASF,CAAM,CAAC,YAAY,CAAC,QAAQ,CAACC,GAE5C,GAAIC,aAAkBC,QAClB,MAAM,IAAIP,EAA8B,CAAEI,OAAAA,EAAQC,MAAAA,CAAM,GAG5D,GAAI,WAAYC,GAAUA,EAAO,MAAM,CACnC,MAAM,IAAIJ,EAAwB,CAAE,cAAeI,EAAO,MAAM,CAAED,MAAAA,CAAM,GAG5E,OAAOC,EAAO,KAAK,AACvB,CClBA,IAAME,EAAe,CAIjBC,EACAC,KAIA,IAAMC,EAAWC,SACXC,EAAcD,SACdE,EAAcF,SAEdG,EAAc,AAACC,GACV,CAAC,CAACA,GAAU,AAAkB,U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,U"}
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "zod",
16
16
  "valibot"
17
17
  ],
18
- "version": "3.0.0",
18
+ "version": "3.0.1",
19
19
  "type": "module",
20
20
  "repository": {
21
21
  "type": "git",