Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F3010096
enum.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.ts
View Options
import
{
is
,
predicates
}
from
"@sealcode/ts-predicates"
;
import
{
Context
}
from
"koa"
;
import
{
FormDataValue
}
from
"../form"
;
import
{
FieldParseResult
,
FormField
}
from
"./field"
;
export
class
EnumField
<
Options
extends
readonly
string
[],
Required
extends
boolean
>
extends
FormField
<
Required
,
string
>
{
constructor
(
required
:
Required
,
public
options
:
Options
,
public
error_message
:
(
options
:
Options
)
=>
string
=
(
options
)
=>
options
.
join
(
", "
)
)
{
super
(
required
);
}
predicate
=
predicates
.
enum
<
Options
>
(
this
.
options
);
public
async
isValueValid
(
_
:
Context
,
value
:
unknown
)
:
Promise
<
{
valid
:
boolean
;
message
:
string
}
>
{
if
(
!
is
(
value
,
predicates
.
string
))
{
return
{
valid
:
false
,
message
:
"expected a string"
};
}
if
(
this
.
options
.
includes
(
value
))
{
return
{
valid
:
true
,
message
:
""
};
}
return
{
valid
:
false
,
message
:
this
.
error_message
(
this
.
options
)
};
}
async
parse
(
_
:
Context
,
value
:
FormDataValue
)
:
Promise
<
FieldParseResult
<
string
>>
{
if
(
is
(
value
,
predicates
.
string
))
{
return
{
parsable
:
true
,
parsed_value
:
value
,
error
:
null
,
};
}
else
{
return
{
parsable
:
false
,
parsed_value
:
null
,
error
:
"Please enter a string"
,
};
}
}
public
getEmptyValue
()
:
string
{
return
""
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Wed, May 7, 19:36 (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
635386
Default Alt Text
enum.ts (1 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment