Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F9584157
resource-type.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
resource-type.js
View Options
var
Promise
=
require
(
"bluebird"
);
var
merge
=
require
(
"merge"
);
var
Chip
=
require
(
"./chip.js"
);
var
ResourceTypeField
=
require
(
"./resource-type-field.js"
);
var
ResourceType
=
function
(
parent_module_path
,
name
,
declaration
){
Chip
.
call
(
this
,
parent_module_path
,
true
,
"resource_type"
,
name
);
this
.
fields
=
{};
this
.
references
=
{};
this
.
keys
=
{};
this
.
accesss_strategy
=
null
;
this
.
_process_declaration
(
declaration
);
}
ResourceType
.
prototype
=
new
function
(){
this
.
_process_declaration
=
function
(
declaration
){
if
(
declaration
){
if
(
declaration
.
fields
){
this
.
add_fields
(
declaration
.
fields
);
}
if
(
declaration
.
access_strategy
){
var
access_strategy_name
=
declaration
.
access_strategy
;
}
else
{
var
access_strategy_name
=
"public"
;
}
this
.
set_access_strategy
(
Sealious
.
ChipManager
.
get_chip
(
"access_strategy"
,
access_strategy_name
));
}
}
this
.
add_field
=
function
(
field_declaration
){
var
field_object
=
new
ResourceTypeField
(
field_declaration
);
var
field_name
=
field_object
.
name
;
if
(
!
this
.
keys
[
field_name
]){
this
.
fields
[
field_name
]
=
field_object
;
this
.
keys
[
field_name
]
=
field_object
;
}
}
this
.
add_fields
=
function
(
field_declarations_array
){
for
(
var
i
in
field_declarations_array
){
var
declaration
=
field_declarations_array
[
i
];
this
.
add_field
(
declaration
);
}
}
function
ensure_no_unknown_fields
(
values
){
var
validation_errors
=
{};
for
(
var
field_name
in
values
){
if
(
this
.
keys
[
field_name
]
==
undefined
){
validation_errors
[
field_name
]
=
"unknown_field"
;
}
}
return
validation_errors
;
}
function
ensure_no_missing_required_fields
(
values
){
var
validation_errors
=
{};
for
(
var
i
in
this
.
keys
){
var
field
=
this
.
keys
[
i
];
if
(
field
.
required
&&
!
values
[
field
.
name
]){
validation_errors
[
field
.
name
]
=
"missing_value"
;
}
}
return
validation_errors
;
}
this
.
validate_field_values
=
function
(
values
){
var
that
=
this
;
var
validation_errors
=
{};
return
new
Promise
(
function
(
resolve
,
reject
){
validation_errors
=
merge
(
validation_errors
,
ensure_no_unknown_fields
.
call
(
that
,
values
));
validation_errors
=
merge
(
validation_errors
,
ensure_no_missing_required_fields
.
call
(
that
,
values
));
var
promise_array
=
[];
for
(
var
key
in
values
){
if
(
validation_errors
[
key
]
===
undefined
){
var
temp_promise
=
that
.
keys
[
key
].
isProperValue
(
values
[
key
]);
promise_array
.
push
(
temp_promise
);
}
}
var
error_message
=
"There are problems with some of the provided values"
;
//TODO: ładnie generować tekst podsumowujący problem z inputem
Promise
.
all
(
promise_array
)
.
then
(
function
(
result
){
if
(
Object
.
keys
(
validation_errors
).
length
>
0
){
throw
new
Sealious
.
Errors
.
ValidationError
(
error_message
,
{
invalid_fields
:
validation_errors
});
}
else
{
resolve
();
}
})
.
catch
(
function
(
error
){
validation_errors
=
merge
(
validation_errors
,
error
);
reject
(
new
Sealious
.
Errors
.
ValidationError
(
error_message
,
{
invalid_fields
:
validation_errors
}));
});
})
}
this
.
encode_field_values
=
function
(
context
,
body
){
var
promises
=
[];
for
(
var
field_name
in
body
){
var
current_value
=
body
[
field_name
];
promises
.
push
(
this
.
keys
[
field_name
].
encode_value
(
context
,
current_value
,
true
));
}
return
Promise
.
all
(
promises
).
then
(
function
(
responses
){
return
new
Promise
(
function
(
resolve
,
reject
){
resolve
(
merge
.
apply
(
merge
,
responses
));
})
});
}
this
.
get_signature
=
function
(){
var
resource_type_signature
=
[];
for
(
var
field_name
in
this
.
fields
){
var
field_signature
=
this
.
fields
[
field_name
].
get_signature
();
resource_type_signature
.
push
(
field_signature
);
}
return
resource_type_signature
;
}
this
.
set_access_strategy
=
function
(
access_strategy
){
this
.
access_strategy
=
access_strategy
;
}
}
ResourceType
.
is_a_constructor
=
false
;
module
.
exports
=
ResourceType
;
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Oct 11, 11:54 (15 s ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
984330
Default Alt Text
resource-type.js (3 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment