Skip to content

Commit

Permalink
fix(Attribute): значение атрибута не должно быть undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
wmakeev committed Jul 29, 2022
1 parent ffd593c commit 0f70fcc
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/model/Attribute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PartialNullable } from '../tools'
import type { NullablePartial } from '../tools'
import type { Entity } from './Entity'
import type { EntityPatchRef, EntityRef } from './EntityRef'
import type { MediaType } from './MediaType'
Expand Down Expand Up @@ -92,15 +92,15 @@ export type Attribute<T extends AttributeType = AttributeType> =
export type AttributePatch<T extends AttributeType = AttributeType> =
T extends AttributeType.File
? EntityPatchRef<'attributemetadata'> & {
file?: {
file: {
filename: string
content: string
}
} | null
}

: T extends AttributeType.CustomEntity
? EntityPatchRef<'attributemetadata'> & {
value: EntityRef<'customentity'>
}

: EntityPatchRef<'attributemetadata'> & PartialNullable<Pick<Attribute<T>, 'value'>>
: EntityPatchRef<'attributemetadata'> & NullablePartial<Pick<Attribute<T>, 'value'>>
35 changes: 35 additions & 0 deletions test/model/attributes.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expectType, expectNotType } from 'tsd'
import { Attribute, AttributePatch, AttributeType } from '../../src'

const t10_1 = {} as Attribute

if (t10_1.type === AttributeType.Boolean) {
expectType<boolean>(t10_1.value)
}

const t11_1 = {} as AttributePatch

expectType<'attributemetadata'>(t11_1.meta.type)

// @ts-expect-error value maybe undefined if file specified
t11_1.value

if ('value' in t11_1) {
// @ts-expect-error Поле `file` не определено если указано `value`
t11_1.file

// value не должен быть undefined, если он указан
expectType<never>({} as any as Extract<typeof t11_1['value'], undefined>)

expectType<null>({} as any as Extract<typeof t11_1['value'], null>)
}

if ('file' in t11_1) {
// @ts-expect-error Поле `value` не определено если указано `file`
t11_1.value

// value не должен быть undefined, если он указан
expectType<never>({} as any as Extract<typeof t11_1['file'], undefined>)

expectType<null>({} as any as Extract<typeof t11_1['file'], null>)
}
52 changes: 51 additions & 1 deletion test/model/utils/Patch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ t1.attributes = [
}
]

t1.attributes = [
// @ts-expect-error Нельзя указывать атрибуты без значений
{
meta: {
type: 'attributemetadata',
href: ''
}
}
]

t1.attributes = [
{
meta: {
Expand All @@ -71,7 +81,22 @@ t1.attributes = [
meta: {
type: 'attributemetadata',
href: ''
}
},
file: null
},
{
meta: {
type: 'attributemetadata',
href: ''
},
value: 'foo'
},
{
meta: {
type: 'attributemetadata',
href: ''
},
value: null
}
]

Expand Down Expand Up @@ -128,7 +153,32 @@ const t4: PatchCollection<'customerorder'> = [
meta: {
type: 'attributemetadata',
href: ''
},
value: true
},
{
meta: {
type: 'attributemetadata',
href: ''
},
value: null
},
{
meta: {
type: 'attributemetadata',
href: ''
},
file: {
filename: 'foo.jpg',
content: ''
}
},
{
meta: {
type: 'attributemetadata',
href: ''
},
file: null
}
],
positions: [
Expand Down

0 comments on commit 0f70fcc

Please sign in to comment.