Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F7113069
field.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
field.ts
View Options
import
{
BaseContext
}
from
"koa"
;
import
{
t
}
from
"../../utils/translate"
;
import
{
FormDataValue
}
from
"../form"
;
export
type
FormFieldValidationResponse
=
{
valid
:
boolean
;
message
:
string
};
export
type
FormFieldValidationFn
<
ValueType
>
=
(
ctx
:
BaseContext
,
value
:
ValueType
,
field
:
FormField
<
ValueType
>
)
=>
Promise
<
FormFieldValidationResponse
>
;
export
type
FieldParsedValue
<
T
>
=
T
extends
FormField
<
infer
R
>
?
R
:
never
;
export
type
FieldParseResult
<
T
>
=
|
{
parsable
:
false
;
error
:
string
;
parsed_value
:
null
;
}
|
{
parsable
:
true
;
error
:
null
;
parsed_value
:
T
};
export
abstract
class
FormField
<
ParsedValue
extends
unknown
=
unknown
>
{
name
:
string
;
constructor
(
public
required
:
boolean
=
false
)
{}
init
(
fieldname
:
string
)
:
void
{
this
.
name
=
fieldname
;
}
public
async
_validate
(
ctx
:
BaseContext
,
value
:
ParsedValue
)
:
Promise
<
FormFieldValidationResponse
>
{
if
(
this
.
required
&&
(
value
===
""
||
value
===
null
||
value
===
undefined
)
)
{
return
{
valid
:
false
,
message
:
t
(
ctx
,
"field_is_required"
,
[],
"This field is required"
),
};
}
return
this
.
isValueValid
(
ctx
,
value
);
}
public
abstract
getEmptyValue
()
:
ParsedValue
;
public
async
isValueValid
(
ctx
:
BaseContext
,
value
:
ParsedValue
)
:
Promise
<
FormFieldValidationResponse
>
{
return
{
valid
:
true
,
message
:
""
};
}
abstract
parse
(
ctx
:
BaseContext
,
raw_value
:
FormDataValue
)
:
Promise
<
FieldParseResult
<
ParsedValue
>>
;
async
getValue
(
ctx
:
BaseContext
,
all_form_values
:
Record
<
string
,
FormDataValue
>
)
:
Promise
<
{
valid
:
boolean
;
message
:
string
;
parsed
:
ParsedValue
;
raw
:
FormDataValue
;
}
>
{
if
(
Object
.
keys
(
all_form_values
).
length
===
0
)
{
// don't validate on virgin form
return
{
parsed
:
this
.
getEmptyValue
(),
valid
:
true
,
message
:
""
,
raw
:
""
,
};
}
const
raw
=
all_form_values
[
this
.
name
];
const
{
parsable
,
parsed_value
,
error
:
parse_error
,
}
=
await
this
.
parse
(
ctx
,
raw
);
if
(
!
parsable
)
{
return
{
valid
:
false
,
parsed
:
this
.
getEmptyValue
(),
raw
,
message
:
parse_error
||
"Error"
,
};
}
const
{
valid
,
message
}
=
await
this
.
_validate
(
ctx
,
parsed_value
as
ParsedValue
);
return
{
parsed
:
parsed_value
as
ParsedValue
,
valid
,
message
,
raw
:
all_form_values
[
this
.
name
],
};
}
}
export
*
from
"./simple-form-field"
;
export
*
from
"./checkboxed-list"
;
export
*
from
"./collection-field"
;
export
*
from
"./number"
;
export
*
from
"./pick-from-list"
;
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Fri, Jul 4, 08:38 (5 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
798849
Default Alt Text
field.ts (2 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment