-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
acceptEmptyString.mjs
63 lines (56 loc) · 1.26 KB
/
acceptEmptyString.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {CAUSE} from "../libs/constants";
import BaseSchema from "../libs/BaseSchema";
import ValueSchemaError from "../libs/ValueSchemaError";
export default BaseSchema.decoratorBuilder(_fit)
.init(_init)
.features({
acceptEmptyString: _featureAcceptEmptyString,
})
.build();
/**
* @package
* @typedef {Params} Params-AcceptEmptyString
* @property {boolean} flag
* @property {*|null} valueOnEmpty
*/
/**
* init
* @param {Params-AcceptEmptyString} params parameters
* @returns {void}
*/
function _init(params)
{
params.flag = false;
}
/**
* accept empty string
* @param {Params-AcceptEmptyString} params parameters
* @param {*} [value=null] value on empty
* @returns {void}
*/
function _featureAcceptEmptyString(params, value = null)
{
params.flag = true;
params.valueOnEmpty = value;
}
/**
* fit
* @param {Params-AcceptEmptyString} params parameters
* @param {Decorator-Values} values original / fitted values
* @param {Key[]} keyStack path to key that caused error
* @returns {boolean} ends fitting
* @throws {ValueSchemaError}
*/
function _fit(params, values, keyStack)
{
if(values.fitted !== "")
{
return false;
}
if(params.flag)
{
values.fitted = params.valueOnEmpty;
return true;
}
ValueSchemaError.raise(CAUSE.EMPTY, values, keyStack);
}