Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22396931
structured.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
structured.ts
View Options
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-explicit-any */
import
{
is
,
predicates
}
from
"@sealcode/ts-predicates"
;
import
{
CollectionItem
,
ExtractFieldDecoded
,
ExtractFieldInput
,
Field
as
SealiousField
,
FieldTypes
as
SealiousFieldTypes
,
}
from
"sealious"
;
import
{
FieldParseResult
,
FormField
}
from
"./field.js"
;
import
{
FieldGroup
,
FormControl
}
from
"../controls/controls.js"
;
import
{
Context
}
from
"koa"
;
import
{
FormDataValue
}
from
"../form-types.js"
;
import
{
sealiousFieldToFormField
}
from
"./collection-field.js"
;
import
{
ExtractedFieldInfo
}
from
"../../utils/extract-fields-from-collection.js"
;
export
class
StructuredSealiousField
<
Structure
extends
Record
<
string
,
SealiousField
<
any
>>
,
F
extends
SealiousFieldTypes
.
Structured
<
Structure
>
,
>
extends
FormField
<
false
,
ExtractFieldDecoded
<
F
>
,
FormControl
,
ExtractFieldInput
<
F
>
>
{
constructor
(
public
sealiousField
:
F
)
{
super
(
false
);
}
public
getEmptyValue
()
:
ExtractFieldDecoded
<
F
>
{
return
{}
as
ExtractFieldDecoded
<
F
>
;
}
async
parse
(
ctx
:
Context
,
raw_value
:
FormDataValue
)
:
Promise
<
FieldParseResult
<
ExtractFieldDecoded
<
F
>>>
{
const
sealiousField
=
this
.
sealiousField
as
SealiousFieldTypes
.
Structured
<
Record
<
string
,
SealiousField
<
any
>>
>
;
if
(
!
is
(
raw_value
,
predicates
.
object
))
{
raw_value
=
{};
}
const
results
=
await
Promise
.
all
(
Object
.
entries
(
sealiousField
.
fields
).
map
(
async
([
key
,
field
])
=>
{
const
formfield
=
sealiousFieldToFormField
(
field
,
key
,
this
.
name
);
return
<
const
>
[
key
,
await
formfield
.
parse
(
ctx
,
((
raw_value
as
Record
<
string
,
unknown
>
)[
key
]
||
""
)
as
any
),
];
})
);
const
parsable
=
results
.
every
(([,
r
])
=>
r
.
parsable
);
if
(
parsable
)
{
return
{
parsable
,
error
:
null
,
parsed_value
:
Object
.
fromEntries
(
results
.
map
(([
key
,
value
])
=>
[
key
,
value
.
parsed_value
])
)
as
any
,
};
}
else
return
{
parsable
:
<
const
>
false
,
parsed_value
:
null
,
error
:
results
.
map
(([,
r
])
=>
r
.
error
).
join
(
"; "
),
};
}
async
parsedToSealious
(
parsed
:
Record
<
string
,
unknown
>
|
null
)
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return
Object
.
fromEntries
(
await
Promise
.
all
(
Object
.
entries
(
parsed
as
Record
<
string
,
any
>
).
map
(
async
([
key
,
value
]
:
[
string
,
any
])
=>
{
const
sealious_field
=
this
.
sealiousField
.
fields
[
key
]
!
;
const
formfield
=
sealiousFieldToFormField
(
sealious_field
,
key
,
this
.
name
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return
[
key
,
await
formfield
.
parsedToSealious
(
value
)];
}
)
)
);
}
async
sealiousValueToForm
(
ctx
:
Context
,
sealiousValue
:
ExtractFieldDecoded
<
F
>
|
ExtractFieldInput
<
F
>
|
null
,
item
:
CollectionItem
<
any
>
)
:
Promise
<
FormDataValue
>
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return
Object
.
fromEntries
(
await
Promise
.
all
(
Object
.
entries
(
(
sealiousValue
||
{})
as
Record
<
string
,
any
>
).
map
(
async
([
key
,
value
]
:
[
string
,
any
])
=>
{
const
sealious_field
=
this
.
sealiousField
.
fields
[
key
]
!
;
const
formfield
=
sealiousFieldToFormField
(
sealious_field
,
key
,
this
.
name
);
return
[
key
,
await
formfield
.
sealiousValueToForm
(
ctx
,
value
,
item
),
];
})
)
);
}
getControl
()
{
const
structured
=
this
.
sealiousField
;
return
new
FieldGroup
(
Object
.
keys
(
structured
.
fields
).
map
((
key
)
=>
{
return
sealiousFieldToFormField
(
structured
.
fields
[
key
]
!
,
key
,
this
.
name
)
.
setLabel
(
key
)
.
getControl
();
}),
{
label
:
this
.
name
}
);
}
generateFieldDeclaration
(
_field_info
:
ExtractedFieldInfo
,
vars
:
{
form_field_types
:
string
;
sealious_field
:
string
}
)
:
string
{
return
`new
${
vars
.
form_field_types
}
.
${
this
.
constructor
.
name
}
(
${
vars
.
sealious_field
}
)`
;
}
}
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Fri, Nov 27, 17:01 (1 d, 21 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1190192
Default Alt Text
structured.ts (3 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline