Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F9582808
enum-multiple.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
enum-multiple.ts
View Options
import
{
is
,
predicates
}
from
"@sealcode/ts-predicates"
;
import
{
Field
,
Context
}
from
"../../../main.js"
;
import
{
ArrayStorage
}
from
"./array-storage.js"
;
import
{
OpenApiTypeMapping
,
OpenApiTypes
,
}
from
"../../../schemas/open-api-types.js"
;
export
class
EnumMultiple
<
Values
extends
string
>
extends
ArrayStorage
<
Values
>
{
typeName
=
"enum-multiple"
;
open_api_type
=
OpenApiTypes
.
NONE
;
// array - custom type generation
async
getOpenApiSchema
(
context
:
Context
)
:
Promise
<
Record
<
string
,
unknown
>>
{
return
{
type
:
"array"
,
items
:
{
...
OpenApiTypeMapping
[
OpenApiTypes
.
STR
],
enum
:
await
this
.
getAllowedValues
(
context
),
},
};
}
constructor
(
public
allowed_values
:
Values
[]
|
((
context
:
Context
)
=>
Values
[])
)
{
super
(
predicates
.
string
);
}
async
getEmptyElement
(
context
:
Context
)
:
Promise
<
Values
>
{
const
allowedValues
=
(
await
this
.
getAllowedValues
(
context
))[
0
];
if
(
allowedValues
)
{
return
allowedValues
;
}
else
{
throw
new
Error
(
"No allowed values found for the provided context"
);
}
}
async
getAllowedValues
(
context
:
Context
)
:
Promise
<
Values
[]
>
{
if
(
typeof
this
.
allowed_values
===
"function"
)
{
return
this
.
allowed_values
(
context
);
}
else
{
return
this
.
allowed_values
;
}
}
async
isProperValue
(
ctx
:
Context
,
value
:
string
[])
{
if
(
!
is
(
value
,
predicates
.
array
(
predicates
.
string
)))
{
return
Field
.
invalid
(
"Not an array of strings"
);
}
const
allowed_values
=
await
this
.
getAllowedValues
(
ctx
);
const
bad_values
=
value
.
filter
(
(
v
)
=>
!
allowed_values
.
includes
(
v
as
Values
)
);
if
(
bad_values
.
length
)
{
return
Field
.
invalid
(
`Invalid values:
${
bad_values
.
join
(
","
)
}
`
);
}
return
Field
.
valid
();
}
async
encode
(
_
:
Context
,
value
:
Values
[])
{
return
value
;
}
getPostgreSqlFieldDefinitions
()
:
string
[]
{
return
[
`"
${
this
.
name
}
" VARCHAR[]`
];
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Sat, Oct 11, 07:49 (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
982018
Default Alt Text
enum-multiple.ts (1 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment