diff --git a/package.json b/package.json index 68caff1..87ce35c 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "preview": "vite preview", "build-only": "vite build -l error", "type-check": "vue-tsc --noEmit", - "test:vitest": "vitest", + "test": "vitest", "postversion": "git push && git push --tags" }, "dependencies": { diff --git a/src/use/metadata.ts b/src/use/metadata.ts index ebd4929..1648259 100644 --- a/src/use/metadata.ts +++ b/src/use/metadata.ts @@ -251,7 +251,7 @@ export function makeDto(requestDto:string, obj?:any, ctx:{ createResponse?:() => return new dtoCtor(obj) } -/** Convert Request DTO values to supported HTML Input values */ +/** Mutates Request DTO values to supported HTML Input values */ export function toFormValues(dto:any, metaType?:MetadataType|null) { if (!dto) return {} Object.keys(dto).forEach((key:string) => { @@ -259,7 +259,7 @@ export function toFormValues(dto:any, metaType?:MetadataType|null) { if (typeof value == 'string') { if (value.startsWith('/Date')) dto[key] = dateInputFormat(toDate(value)) - } else if (typeof value == 'object') { + } else if (value != null && typeof value == 'object') { // shallow clone if (Array.isArray(value)) { dto[key] = Array.from(value) diff --git a/tests/unit/Metadata.spec.ts b/tests/unit/Metadata.spec.ts new file mode 100644 index 0000000..00375ce --- /dev/null +++ b/tests/unit/Metadata.spec.ts @@ -0,0 +1,15 @@ +import { createApp,h } from 'vue' +import { test, expect} from 'vitest' +import { useMetadata } from '../../src/index' + +test('toFormValues', () => { + const { toFormValues } = useMetadata() + const a = { a: "foo", b: 1, c:null } + const b = toFormValues(a) + + // toFormValues mutates and returns original argument + expect(a === b).true + expect(a.a).eq('foo') + expect(a.b).eq(1) + expect(a.c).eq(null) +})