@mionjs/type-formats 0.8.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dist/cjs/BigintFormats.cjs +14 -0
- package/.dist/cjs/BigintFormats.cjs.map +1 -0
- package/.dist/cjs/BigintFormats.d.ts +2 -0
- package/.dist/cjs/NumberFormats.cjs +20 -0
- package/.dist/cjs/NumberFormats.cjs.map +1 -0
- package/.dist/cjs/NumberFormats.d.ts +2 -0
- package/.dist/cjs/StringFormats.cjs +120 -0
- package/.dist/cjs/StringFormats.cjs.map +1 -0
- package/.dist/cjs/StringFormats.d.ts +11 -0
- package/.dist/cjs/constants.d.ts +1 -0
- package/.dist/cjs/package.json +1 -0
- package/.dist/cjs/src/bigint/bigIntFormat.runtype.cjs +152 -0
- package/.dist/cjs/src/bigint/bigIntFormat.runtype.cjs.map +1 -0
- package/.dist/cjs/src/bigint/bigIntFormat.runtype.d.ts +21 -0
- package/.dist/cjs/src/bigint/defaultBigNumberFormats.cjs +16 -0
- package/.dist/cjs/src/bigint/defaultBigNumberFormats.cjs.map +1 -0
- package/.dist/cjs/src/bigint/defaultBigNumberFormats.d.ts +29 -0
- package/.dist/cjs/src/constants.cjs +34 -0
- package/.dist/cjs/src/constants.cjs.map +1 -0
- package/.dist/cjs/src/constants.d.ts +15 -0
- package/.dist/cjs/src/constants.mock.cjs +111 -0
- package/.dist/cjs/src/constants.mock.cjs.map +1 -0
- package/.dist/cjs/src/constants.mock.d.ts +335 -0
- package/.dist/cjs/src/number/defaultNumberFormats.cjs +28 -0
- package/.dist/cjs/src/number/defaultNumberFormats.cjs.map +1 -0
- package/.dist/cjs/src/number/defaultNumberFormats.d.ts +63 -0
- package/.dist/cjs/src/number/numberFormat.runtype.cjs +214 -0
- package/.dist/cjs/src/number/numberFormat.runtype.cjs.map +1 -0
- package/.dist/cjs/src/number/numberFormat.runtype.d.ts +21 -0
- package/.dist/cjs/src/string/date.runtype.cjs +88 -0
- package/.dist/cjs/src/string/date.runtype.cjs.map +1 -0
- package/.dist/cjs/src/string/date.runtype.d.ts +19 -0
- package/.dist/cjs/src/string/dateTime.runtype.cjs +83 -0
- package/.dist/cjs/src/string/dateTime.runtype.cjs.map +1 -0
- package/.dist/cjs/src/string/dateTime.runtype.d.ts +26 -0
- package/.dist/cjs/src/string/defaultStringFormats.runtype.cjs +25 -0
- package/.dist/cjs/src/string/defaultStringFormats.runtype.cjs.map +1 -0
- package/.dist/cjs/src/string/defaultStringFormats.runtype.d.ts +45 -0
- package/.dist/cjs/src/string/domain.runtype.cjs +248 -0
- package/.dist/cjs/src/string/domain.runtype.cjs.map +1 -0
- package/.dist/cjs/src/string/domain.runtype.d.ts +72 -0
- package/.dist/cjs/src/string/email.runtype.cjs +159 -0
- package/.dist/cjs/src/string/email.runtype.cjs.map +1 -0
- package/.dist/cjs/src/string/email.runtype.d.ts +57 -0
- package/.dist/cjs/src/string/ip.runtype.cjs +72 -0
- package/.dist/cjs/src/string/ip.runtype.cjs.map +1 -0
- package/.dist/cjs/src/string/ip.runtype.d.ts +50 -0
- package/.dist/cjs/src/string/stringFormat.runtype.cjs +270 -0
- package/.dist/cjs/src/string/stringFormat.runtype.cjs.map +1 -0
- package/.dist/cjs/src/string/stringFormat.runtype.d.ts +21 -0
- package/.dist/cjs/src/string/time.runtype.cjs +100 -0
- package/.dist/cjs/src/string/time.runtype.cjs.map +1 -0
- package/.dist/cjs/src/string/time.runtype.d.ts +21 -0
- package/.dist/cjs/src/string/url.runtype.cjs +160 -0
- package/.dist/cjs/src/string/url.runtype.cjs.map +1 -0
- package/.dist/cjs/src/string/url.runtype.d.ts +81 -0
- package/.dist/cjs/src/string/uuid.runtype.cjs +44 -0
- package/.dist/cjs/src/string/uuid.runtype.cjs.map +1 -0
- package/.dist/cjs/src/string/uuid.runtype.d.ts +22 -0
- package/.dist/cjs/src/type-formats-pure-fns.cjs +390 -0
- package/.dist/cjs/src/type-formats-pure-fns.cjs.map +1 -0
- package/.dist/cjs/src/type-formats-pure-fns.d.ts +27 -0
- package/.dist/cjs/src/utils.cjs +14 -0
- package/.dist/cjs/src/utils.cjs.map +1 -0
- package/.dist/cjs/src/utils.d.ts +3 -0
- package/.dist/cjs/vitest.config.d.ts +2 -0
- package/.dist/esm/BigintFormats.d.ts +2 -0
- package/.dist/esm/BigintFormats.js +14 -0
- package/.dist/esm/BigintFormats.js.map +1 -0
- package/.dist/esm/NumberFormats.d.ts +2 -0
- package/.dist/esm/NumberFormats.js +20 -0
- package/.dist/esm/NumberFormats.js.map +1 -0
- package/.dist/esm/StringFormats.d.ts +11 -0
- package/.dist/esm/StringFormats.js +120 -0
- package/.dist/esm/StringFormats.js.map +1 -0
- package/.dist/esm/constants.d.ts +1 -0
- package/.dist/esm/src/bigint/bigIntFormat.runtype.d.ts +21 -0
- package/.dist/esm/src/bigint/bigIntFormat.runtype.js +152 -0
- package/.dist/esm/src/bigint/bigIntFormat.runtype.js.map +1 -0
- package/.dist/esm/src/bigint/defaultBigNumberFormats.d.ts +29 -0
- package/.dist/esm/src/bigint/defaultBigNumberFormats.js +16 -0
- package/.dist/esm/src/bigint/defaultBigNumberFormats.js.map +1 -0
- package/.dist/esm/src/constants.d.ts +15 -0
- package/.dist/esm/src/constants.js +34 -0
- package/.dist/esm/src/constants.js.map +1 -0
- package/.dist/esm/src/constants.mock.d.ts +335 -0
- package/.dist/esm/src/constants.mock.js +111 -0
- package/.dist/esm/src/constants.mock.js.map +1 -0
- package/.dist/esm/src/number/defaultNumberFormats.d.ts +63 -0
- package/.dist/esm/src/number/defaultNumberFormats.js +28 -0
- package/.dist/esm/src/number/defaultNumberFormats.js.map +1 -0
- package/.dist/esm/src/number/numberFormat.runtype.d.ts +21 -0
- package/.dist/esm/src/number/numberFormat.runtype.js +214 -0
- package/.dist/esm/src/number/numberFormat.runtype.js.map +1 -0
- package/.dist/esm/src/string/date.runtype.d.ts +19 -0
- package/.dist/esm/src/string/date.runtype.js +88 -0
- package/.dist/esm/src/string/date.runtype.js.map +1 -0
- package/.dist/esm/src/string/dateTime.runtype.d.ts +26 -0
- package/.dist/esm/src/string/dateTime.runtype.js +83 -0
- package/.dist/esm/src/string/dateTime.runtype.js.map +1 -0
- package/.dist/esm/src/string/defaultStringFormats.runtype.d.ts +45 -0
- package/.dist/esm/src/string/defaultStringFormats.runtype.js +25 -0
- package/.dist/esm/src/string/defaultStringFormats.runtype.js.map +1 -0
- package/.dist/esm/src/string/domain.runtype.d.ts +72 -0
- package/.dist/esm/src/string/domain.runtype.js +248 -0
- package/.dist/esm/src/string/domain.runtype.js.map +1 -0
- package/.dist/esm/src/string/email.runtype.d.ts +57 -0
- package/.dist/esm/src/string/email.runtype.js +159 -0
- package/.dist/esm/src/string/email.runtype.js.map +1 -0
- package/.dist/esm/src/string/ip.runtype.d.ts +50 -0
- package/.dist/esm/src/string/ip.runtype.js +72 -0
- package/.dist/esm/src/string/ip.runtype.js.map +1 -0
- package/.dist/esm/src/string/stringFormat.runtype.d.ts +21 -0
- package/.dist/esm/src/string/stringFormat.runtype.js +270 -0
- package/.dist/esm/src/string/stringFormat.runtype.js.map +1 -0
- package/.dist/esm/src/string/time.runtype.d.ts +21 -0
- package/.dist/esm/src/string/time.runtype.js +100 -0
- package/.dist/esm/src/string/time.runtype.js.map +1 -0
- package/.dist/esm/src/string/url.runtype.d.ts +81 -0
- package/.dist/esm/src/string/url.runtype.js +160 -0
- package/.dist/esm/src/string/url.runtype.js.map +1 -0
- package/.dist/esm/src/string/uuid.runtype.d.ts +22 -0
- package/.dist/esm/src/string/uuid.runtype.js +44 -0
- package/.dist/esm/src/string/uuid.runtype.js.map +1 -0
- package/.dist/esm/src/type-formats-pure-fns.d.ts +27 -0
- package/.dist/esm/src/type-formats-pure-fns.js +390 -0
- package/.dist/esm/src/type-formats-pure-fns.js.map +1 -0
- package/.dist/esm/src/utils.d.ts +3 -0
- package/.dist/esm/src/utils.js +14 -0
- package/.dist/esm/src/utils.js.map +1 -0
- package/.dist/esm/vitest.config.d.ts +2 -0
- package/LICENSE +21 -0
- package/README.md +123 -0
- package/package.json +80 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const runTypes = require("@mionjs/run-types");
|
|
4
|
+
const type = require("@deepkit/type");
|
|
5
|
+
const src_string_domain_runtype = require("./domain.runtype.cjs");
|
|
6
|
+
const src_string_stringFormat_runtype = require("./stringFormat.runtype.cjs");
|
|
7
|
+
const src_constants_mock = require("../constants.mock.cjs");
|
|
8
|
+
const src_utils = require("../utils.cjs");
|
|
9
|
+
const EMAIL_PATTERN = /^[^\s@]{1,64}@(?:[a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,63}$/;
|
|
10
|
+
const EMAIL_PATTERN_PUNYCODE = /^[^\s@]{1,64}@(?:[a-zA-Z0-9-]{1,63}\.)+[a-zA-Z0-9-]{2,63}$/;
|
|
11
|
+
class EmailRunTypeFormat extends runTypes.BaseRunTypeFormat {
|
|
12
|
+
static id = "email";
|
|
13
|
+
kind = type.ReflectionKind.string;
|
|
14
|
+
name = EmailRunTypeFormat.id;
|
|
15
|
+
// Formatter instances as class variables
|
|
16
|
+
rootFormatter;
|
|
17
|
+
domainFormatter;
|
|
18
|
+
localPartFormatter;
|
|
19
|
+
constructor(parentPath) {
|
|
20
|
+
super(parentPath);
|
|
21
|
+
this.rootFormatter = new src_string_stringFormat_runtype.StringRunTypeFormat(this.getFormatPath());
|
|
22
|
+
this.domainFormatter = new src_string_domain_runtype.DomainRunTypeFormat(this.getFormatPath("domain"));
|
|
23
|
+
this.localPartFormatter = new src_string_stringFormat_runtype.StringRunTypeFormat(this.getFormatPath("localPart"));
|
|
24
|
+
}
|
|
25
|
+
getIgnoredProps() {
|
|
26
|
+
return src_string_stringFormat_runtype.stringIgnoreProps;
|
|
27
|
+
}
|
|
28
|
+
canEmbedFormatterCode(fnID, rt, p) {
|
|
29
|
+
const params = p || this.getParams(rt);
|
|
30
|
+
if (fnID === runTypes.JitFunctions.isType.id || fnID === runTypes.JitFunctions.typeErrors.id) {
|
|
31
|
+
const superResult = super.canEmbedFormatterCode(fnID, rt);
|
|
32
|
+
const domainResult = params.domain ? this.domainFormatter.canEmbedFormatterCode(fnID, rt, params.domain) : false;
|
|
33
|
+
return superResult && (!!params.pattern || domainResult);
|
|
34
|
+
}
|
|
35
|
+
return super.canEmbedFormatterCode(fnID, rt);
|
|
36
|
+
}
|
|
37
|
+
emitIsType(comp, rt) {
|
|
38
|
+
const params = this.getParams(rt);
|
|
39
|
+
const fnID = comp.fnID;
|
|
40
|
+
const fmtName = this.getFormatName();
|
|
41
|
+
if (params.pattern)
|
|
42
|
+
return this.rootFormatter.compileFormat(fnID, comp, rt, params, comp.vλl, fmtName);
|
|
43
|
+
const vλl = comp.vλl;
|
|
44
|
+
const vLocalPart = "localPart" + this.getFormatNestLevel();
|
|
45
|
+
const vDomain = "domain" + this.getFormatNestLevel();
|
|
46
|
+
const vAtPos = "atPos" + this.getFormatNestLevel();
|
|
47
|
+
const rootCode = this.rootFormatter.compileFormat(fnID, comp, rt, params, vλl, fmtName);
|
|
48
|
+
const localPartCode = this.localPartFormatter.compileFormat(fnID, comp, rt, params.localPart, vLocalPart, fmtName);
|
|
49
|
+
const domainCode = this.domainFormatter.compileFormat(fnID, comp, rt, params.domain, vDomain, fmtName);
|
|
50
|
+
const rootSafeCode = rootCode.code ? `if (!(${rootCode.code})) return false;` : "";
|
|
51
|
+
const returnCode = this.isRootFormat() ? `return true;` : "";
|
|
52
|
+
const domainIsExpression = domainCode.type === "E";
|
|
53
|
+
const domainSafeCode = domainCode.code && domainIsExpression ? `if (!(${domainCode.code})) return false;` : domainCode.code;
|
|
54
|
+
const code = `
|
|
55
|
+
${rootSafeCode}
|
|
56
|
+
const ${vAtPos} = ${vλl}.lastIndexOf('@');
|
|
57
|
+
if (${vAtPos} === -1) return false;
|
|
58
|
+
const ${vLocalPart} = ${vλl}.substring(0, ${vAtPos});
|
|
59
|
+
const ${vDomain} = ${vλl}.substring(${vAtPos} + 1);
|
|
60
|
+
if (!(${localPartCode.code})) return false;
|
|
61
|
+
${domainSafeCode}
|
|
62
|
+
${returnCode}
|
|
63
|
+
`;
|
|
64
|
+
return { code, type: "S" };
|
|
65
|
+
}
|
|
66
|
+
emitIsTypeErrors(comp, rt) {
|
|
67
|
+
const params = this.getParams(rt);
|
|
68
|
+
const fnID = comp.fnID;
|
|
69
|
+
const fmtName = this.getFormatName();
|
|
70
|
+
if (params.pattern)
|
|
71
|
+
return this.rootFormatter.compileFormat(fnID, comp, rt, params, comp.vλl, fmtName);
|
|
72
|
+
const errFn = this.getCallJitFormatErr(comp, rt, this, false);
|
|
73
|
+
const vλl = comp.vλl;
|
|
74
|
+
const vLocalPart = "localPart";
|
|
75
|
+
const vDomain = "domain";
|
|
76
|
+
const vAtPos = "atPos";
|
|
77
|
+
const rootCode = this.rootFormatter.compileFormat(fnID, comp, rt, params, vλl, fmtName);
|
|
78
|
+
const localPartCode = this.localPartFormatter.compileFormat(fnID, comp, rt, params.localPart, vLocalPart, fmtName);
|
|
79
|
+
const domainCode = this.domainFormatter.compileFormat(fnID, comp, rt, params.domain, vDomain, fmtName);
|
|
80
|
+
const code = `
|
|
81
|
+
${rootCode.code ? `${rootCode.code};` : ""}
|
|
82
|
+
const ${vAtPos} = ${vλl}.lastIndexOf('@');
|
|
83
|
+
if (${vAtPos} === -1) ${errFn("@", "Email missing @ symbol")};
|
|
84
|
+
const ${vLocalPart} = ${vλl}.substring(0, ${vAtPos});
|
|
85
|
+
const ${vDomain} = ${vλl}.substring(${vAtPos} + 1);
|
|
86
|
+
${localPartCode.code ? `${localPartCode.code};` : ""}
|
|
87
|
+
${domainCode.code ? `${domainCode.code};` : ""}
|
|
88
|
+
`;
|
|
89
|
+
return { code, type: "S" };
|
|
90
|
+
}
|
|
91
|
+
_mock(opts, rt) {
|
|
92
|
+
const params = this.getParams(rt);
|
|
93
|
+
if (params.pattern)
|
|
94
|
+
return this.rootFormatter.mock(opts, rt, params);
|
|
95
|
+
const localPart = this.mockLocalPart(opts, rt, params.localPart);
|
|
96
|
+
const domain = this.domainFormatter.mock(opts, rt, params.domain);
|
|
97
|
+
return `${localPart}@${domain}`;
|
|
98
|
+
}
|
|
99
|
+
mockLocalPart(opts, rt, params) {
|
|
100
|
+
const hasParams = !!Object.keys(params).length;
|
|
101
|
+
if (!hasParams)
|
|
102
|
+
return runTypes.randomItem(src_constants_mock.EMAIL_NAME_SAMPLES_ARRAY);
|
|
103
|
+
const defaultParams = {
|
|
104
|
+
...params,
|
|
105
|
+
maxLength: params.maxLength ?? 64,
|
|
106
|
+
minLength: params.minLength ?? 1
|
|
107
|
+
};
|
|
108
|
+
return this.localPartFormatter.mock(opts, rt, defaultParams);
|
|
109
|
+
}
|
|
110
|
+
emitFormat(comp) {
|
|
111
|
+
return { code: `${comp.vλl}.toLowerCase()`, type: "E" };
|
|
112
|
+
}
|
|
113
|
+
validateParams(rt, params) {
|
|
114
|
+
const hasPattern = !!params.pattern;
|
|
115
|
+
const hasLocalPartOrDomain = !!params.localPart || !!params.domain;
|
|
116
|
+
if (hasPattern && hasLocalPartOrDomain)
|
|
117
|
+
throw new Error(`Email can only have either pattern or (localPart and domain) in type ${this.printPath(rt)}`);
|
|
118
|
+
if (params.localPart && !params.domain || !params.localPart && params.domain)
|
|
119
|
+
throw new Error(`Email localPart and domain must be used together in type ${this.printPath(rt)}`);
|
|
120
|
+
if (params.maxLength && src_utils.paramVal(params.maxLength) > 254)
|
|
121
|
+
throw new Error(`Email maxLength cannot be greater than 254 in type ${this.printPath(rt, "maxLength")}`);
|
|
122
|
+
if (params.minLength && src_utils.paramVal(params.minLength) < 7)
|
|
123
|
+
throw new Error(`Email minLength cannot be less than 7 in type ${this.printPath(rt, "minLength")}`);
|
|
124
|
+
this.rootFormatter.validateParams(rt, params);
|
|
125
|
+
if (params.localPart) {
|
|
126
|
+
if (Object.values(params.localPart).length === 0)
|
|
127
|
+
throw new Error(`Email localPart must have at least one validator in type ${this.printPath(rt, "localPart")}`);
|
|
128
|
+
if (params.localPart.maxLength && src_utils.paramVal(params.localPart.maxLength) > 64)
|
|
129
|
+
throw new Error(`Email localPart.maxLength cannot be greater than 64 in type ${this.printPath(rt, "localPart.maxLength")}`);
|
|
130
|
+
if (params.localPart.minLength && src_utils.paramVal(params.localPart.minLength) < 1)
|
|
131
|
+
throw new Error(`Email localPart.minLength cannot be less than 1 in type ${this.printPath(rt, "localPart.minLength")}`);
|
|
132
|
+
this.localPartFormatter.validateParams(rt, params.localPart);
|
|
133
|
+
}
|
|
134
|
+
if (params.domain) {
|
|
135
|
+
if (Object.values(params.domain).length === 0) {
|
|
136
|
+
throw new Error(`Email domain must have at least one validator in type ${this.printPath(rt, "domain")}`);
|
|
137
|
+
}
|
|
138
|
+
this.domainFormatter.validateParams(rt, params.domain);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const EMAIL_RUN_TYPE_FORMATTER = runTypes.registerFormatter(new EmailRunTypeFormat());
|
|
143
|
+
const __ΩDEFAULT_STRICT_EMAIL_PARAMS = [254, "maxLength", 64, 1, "minLength", ` ()<>[]:;\\,{}|+@`, "val", "Invalid characters in email local part", "errorMessage", () => src_constants_mock.__ΩEMAIL_NAME_SAMPLES, "mockSamples", "disallowedChars", "localPart", () => src_string_domain_runtype.__ΩDEFAULT_STRICT_DOMAIN_PARAMS, "domain", "DEFAULT_STRICT_EMAIL_PARAMS", `P.!4"P.#4".$4%P.&4'.(4)n*4+M4,M4-n.4/Mw0y`];
|
|
144
|
+
const __ΩDEFAULT_EMAIL_PARAMS = [() => EMAIL_PATTERN, "EmailPattern", () => src_constants_mock.__ΩEMAIL_SAMPLES, "MockSamples", 254, "maxLength", 7, "minLength", "val", "mockSamples", "Invalid email format", "errorMessage", "pattern", "DEFAULT_EMAIL_PARAMS", `i!c"n#c$P.%4&.'4(Pe#!4)e#"4*.+4,M4-Mw.y`];
|
|
145
|
+
const __ΩFormatEmail = [() => __ΩDEFAULT_EMAIL_PARAMS, "EP", () => runTypes.__ΩTypeFormat, "email", "email", "FormatEmail", 'n!c"&.$e!!.%o#%w&y'];
|
|
146
|
+
const __ΩFormatEmailStrict = ["E", () => __ΩFormatEmail, () => __ΩDEFAULT_STRICT_EMAIL_PARAMS, "FormatEmailStrict", 'PMc!Pn#e"!Ko""w$y'];
|
|
147
|
+
const __ΩFormatEmailPattern = ["EmailPattern", "MockSamples", () => __ΩFormatEmail, () => __ΩDEFAULT_EMAIL_PARAMS, "FormatEmailPattern", 'b!b"e!!e!"o$#o#"w%y'];
|
|
148
|
+
const __ΩFormatEmailPunycode = [() => __ΩFormatEmailPattern, () => EMAIL_PATTERN_PUNYCODE, () => src_constants_mock.__ΩEMAIL_SAMPLES_PUNYCODE, "FormatEmailPunycode", 'i"n#o!#w$y'];
|
|
149
|
+
exports.EMAIL_PATTERN = EMAIL_PATTERN;
|
|
150
|
+
exports.EMAIL_PATTERN_PUNYCODE = EMAIL_PATTERN_PUNYCODE;
|
|
151
|
+
exports.EMAIL_RUN_TYPE_FORMATTER = EMAIL_RUN_TYPE_FORMATTER;
|
|
152
|
+
exports.EmailRunTypeFormat = EmailRunTypeFormat;
|
|
153
|
+
exports.__ΩDEFAULT_EMAIL_PARAMS = __ΩDEFAULT_EMAIL_PARAMS;
|
|
154
|
+
exports.__ΩDEFAULT_STRICT_EMAIL_PARAMS = __ΩDEFAULT_STRICT_EMAIL_PARAMS;
|
|
155
|
+
exports.__ΩFormatEmail = __ΩFormatEmail;
|
|
156
|
+
exports.__ΩFormatEmailPattern = __ΩFormatEmailPattern;
|
|
157
|
+
exports.__ΩFormatEmailPunycode = __ΩFormatEmailPunycode;
|
|
158
|
+
exports.__ΩFormatEmailStrict = __ΩFormatEmailStrict;
|
|
159
|
+
//# sourceMappingURL=email.runtype.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email.runtype.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { BaseRunType, JitFnCompiler, JitErrorsFnCompiler, StrNumber, JitFnID, JitCode, BaseRunTypeFormat, TypeFormat, RunTypeOptions } from '@mionjs/run-types';
|
|
2
|
+
import { ReflectionKind } from '@deepkit/type';
|
|
3
|
+
import { DEFAULT_STRICT_DOMAIN_PARAMS } from './domain.runtype.ts';
|
|
4
|
+
import { FormatParams_Email, Samples } from '@mionjs/core';
|
|
5
|
+
import { EMAIL_NAME_SAMPLES, EMAIL_SAMPLES, EMAIL_SAMPLES_PUNYCODE } from '../constants.mock.ts';
|
|
6
|
+
export declare const EMAIL_PATTERN: RegExp;
|
|
7
|
+
export declare const EMAIL_PATTERN_PUNYCODE: RegExp;
|
|
8
|
+
export declare class EmailRunTypeFormat extends BaseRunTypeFormat<FormatParams_Email> {
|
|
9
|
+
static id: string;
|
|
10
|
+
kind: ReflectionKind;
|
|
11
|
+
name: string;
|
|
12
|
+
private rootFormatter;
|
|
13
|
+
private domainFormatter;
|
|
14
|
+
private localPartFormatter;
|
|
15
|
+
constructor(parentPath?: StrNumber[]);
|
|
16
|
+
getIgnoredProps(): string[] | undefined;
|
|
17
|
+
canEmbedFormatterCode(fnID: JitFnID, rt: BaseRunType, p?: FormatParams_Email): boolean;
|
|
18
|
+
emitIsType(comp: JitFnCompiler, rt: BaseRunType): JitCode;
|
|
19
|
+
emitIsTypeErrors(comp: JitErrorsFnCompiler, rt: BaseRunType): JitCode;
|
|
20
|
+
_mock(opts: RunTypeOptions, rt: BaseRunType): string;
|
|
21
|
+
private mockLocalPart;
|
|
22
|
+
emitFormat(comp: JitFnCompiler): JitCode;
|
|
23
|
+
validateParams(rt: BaseRunType, params: FormatParams_Email): void;
|
|
24
|
+
}
|
|
25
|
+
export declare const EMAIL_RUN_TYPE_FORMATTER: EmailRunTypeFormat;
|
|
26
|
+
export type DEFAULT_STRICT_EMAIL_PARAMS = {
|
|
27
|
+
maxLength: 254;
|
|
28
|
+
localPart: {
|
|
29
|
+
maxLength: 64;
|
|
30
|
+
minLength: 1;
|
|
31
|
+
disallowedChars: {
|
|
32
|
+
val: ` ()<>[]:;\\,{}|+@`;
|
|
33
|
+
errorMessage: 'Invalid characters in email local part';
|
|
34
|
+
mockSamples: EMAIL_NAME_SAMPLES;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
domain: DEFAULT_STRICT_DOMAIN_PARAMS;
|
|
38
|
+
};
|
|
39
|
+
export type DEFAULT_EMAIL_PARAMS<EmailPattern extends RegExp = typeof EMAIL_PATTERN, MockSamples extends Samples = EMAIL_SAMPLES> = {
|
|
40
|
+
maxLength: 254;
|
|
41
|
+
minLength: 7;
|
|
42
|
+
pattern: {
|
|
43
|
+
val: EmailPattern;
|
|
44
|
+
mockSamples: MockSamples;
|
|
45
|
+
errorMessage: 'Invalid email format';
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
export type FormatEmail<EP extends FormatParams_Email = DEFAULT_EMAIL_PARAMS> = TypeFormat<string, 'email', EP, 'email'>;
|
|
49
|
+
export type FormatEmailStrict<E extends Partial<FormatParams_Email> = {}> = FormatEmail<DEFAULT_STRICT_EMAIL_PARAMS & E>;
|
|
50
|
+
export type FormatEmailPattern<EmailPattern extends RegExp, MockSamples extends Samples> = FormatEmail<DEFAULT_EMAIL_PARAMS<EmailPattern, MockSamples>>;
|
|
51
|
+
export type FormatEmailPunycode = FormatEmailPattern<typeof EMAIL_PATTERN_PUNYCODE, EMAIL_SAMPLES_PUNYCODE>;
|
|
52
|
+
export declare type __ΩDEFAULT_STRICT_EMAIL_PARAMS = any[];
|
|
53
|
+
export declare type __ΩDEFAULT_EMAIL_PARAMS = any[];
|
|
54
|
+
export declare type __ΩFormatEmail = any[];
|
|
55
|
+
export declare type __ΩFormatEmailStrict = any[];
|
|
56
|
+
export declare type __ΩFormatEmailPattern = any[];
|
|
57
|
+
export declare type __ΩFormatEmailPunycode = any[];
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const runTypes = require("@mionjs/run-types");
|
|
4
|
+
const type = require("@deepkit/type");
|
|
5
|
+
const src_utils = require("../utils.cjs");
|
|
6
|
+
const src_typeFormatsPureFns = require("../type-formats-pure-fns.cjs");
|
|
7
|
+
class IPRunTypeFormat extends runTypes.BaseRunTypeFormat {
|
|
8
|
+
static id = "ip";
|
|
9
|
+
kind = type.ReflectionKind.string;
|
|
10
|
+
name = IPRunTypeFormat.id;
|
|
11
|
+
emitIsType(comp, rt) {
|
|
12
|
+
const params = this.getParams(rt);
|
|
13
|
+
if (params.version === 4)
|
|
14
|
+
return { code: this.compilePureFunctionCall(comp, rt, src_typeFormatsPureFns.cpf_isIPV4).callCode, type: "E" };
|
|
15
|
+
if (params.version === 6)
|
|
16
|
+
return { code: this.compilePureFunctionCall(comp, rt, src_typeFormatsPureFns.cpf_isIPV6).callCode, type: "E" };
|
|
17
|
+
return {
|
|
18
|
+
code: `${this.compilePureFunctionCall(comp, rt, src_typeFormatsPureFns.cpf_isIPV4).callCode} || ${this.compilePureFunctionCall(comp, rt, src_typeFormatsPureFns.cpf_isIPV6).callCode}`,
|
|
19
|
+
type: "E"
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
_mock(opts, rt) {
|
|
23
|
+
const params = this.getParams(rt);
|
|
24
|
+
if (params.version === 4)
|
|
25
|
+
return mockIpV4(params);
|
|
26
|
+
if (params.version === 6)
|
|
27
|
+
return mockIpV6(params);
|
|
28
|
+
return Math.random() > 0.5 ? mockIpV4(params) : mockIpV6(params);
|
|
29
|
+
}
|
|
30
|
+
emitIsTypeErrors(comp, rt) {
|
|
31
|
+
const isTypeCodeObj = this.emitIsType(comp, rt);
|
|
32
|
+
const isTypeCode = isTypeCodeObj.code;
|
|
33
|
+
if (!isTypeCode)
|
|
34
|
+
return { code: "", type: "S" };
|
|
35
|
+
const params = this.getParams(rt);
|
|
36
|
+
const errFn = this.getCallJitFormatErr(comp, rt, this);
|
|
37
|
+
return { code: `if (!(${isTypeCode})) ${errFn("version", src_utils.paramVal(params.version))}`, type: "S" };
|
|
38
|
+
}
|
|
39
|
+
emitFormat(comp) {
|
|
40
|
+
return { code: `${comp.vλl}.toLowerCase()`, type: "E" };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function mockIpV4(p) {
|
|
44
|
+
const r = Math.random();
|
|
45
|
+
if (p.allowLocalHost && r > 0.8)
|
|
46
|
+
return Math.random() > 0.5 ? "localhost" : "127:0:0:1";
|
|
47
|
+
return Array.from({ length: 4 }, () => Math.floor(Math.random() * 256)).join(".");
|
|
48
|
+
}
|
|
49
|
+
function mockIpV6(p) {
|
|
50
|
+
if (p.allowLocalHost && Math.random() > 0.8)
|
|
51
|
+
return Math.random() > 0.5 ? "0:0:0:0:0:0:0:1" : "::1";
|
|
52
|
+
return Array.from({ length: 8 }, () => Math.floor(Math.random() * 65535).toString(16)).join(":");
|
|
53
|
+
}
|
|
54
|
+
const IP_RUN_TYPE_FORMATTER = runTypes.registerFormatter(new IPRunTypeFormat());
|
|
55
|
+
const __ΩDEFAULT_IP_PARAMS = ["any", "version", true, "allowLocalHost", "DEFAULT_IP_PARAMS", 'P.!4".#4$Mw%y'];
|
|
56
|
+
const __ΩFormatIP = [() => __ΩDEFAULT_IP_PARAMS, "P", () => runTypes.__ΩTypeFormat, "ip", "ip", "FormatIP", 'n!c"&.$e!!.%o#%w&y'];
|
|
57
|
+
const __ΩFormatIPv4 = [() => __ΩFormatIP, 4, "version", true, "allowLocalHost", "FormatIPv4", 'P."4#.$4%Mo!"w&y'];
|
|
58
|
+
const __ΩFormatIPv6 = [() => __ΩFormatIP, 6, "version", true, "allowLocalHost", "FormatIPv6", 'P."4#.$4%Mo!"w&y'];
|
|
59
|
+
const __ΩFormatIPWithPort = [() => __ΩFormatIP, "any", "version", true, "allowLocalHost", true, "allowPort", "FormatIPWithPort", `P."4#.$4%.&4'Mo!"w(y`];
|
|
60
|
+
const __ΩFormatIPv4WithPort = [() => __ΩFormatIP, 4, "version", true, "allowLocalHost", true, "allowPort", "FormatIPv4WithPort", `P."4#.$4%.&4'Mo!"w(y`];
|
|
61
|
+
const __ΩFormatIPv6WithPort = [() => __ΩFormatIP, 6, "version", true, "allowLocalHost", true, "allowPort", "FormatIPv6WithPort", `P."4#.$4%.&4'Mo!"w(y`];
|
|
62
|
+
exports.IPRunTypeFormat = IPRunTypeFormat;
|
|
63
|
+
exports.IP_RUN_TYPE_FORMATTER = IP_RUN_TYPE_FORMATTER;
|
|
64
|
+
exports.__ΩFormatIP = __ΩFormatIP;
|
|
65
|
+
exports.__ΩFormatIPWithPort = __ΩFormatIPWithPort;
|
|
66
|
+
exports.__ΩFormatIPv4 = __ΩFormatIPv4;
|
|
67
|
+
exports.__ΩFormatIPv4WithPort = __ΩFormatIPv4WithPort;
|
|
68
|
+
exports.__ΩFormatIPv6 = __ΩFormatIPv6;
|
|
69
|
+
exports.__ΩFormatIPv6WithPort = __ΩFormatIPv6WithPort;
|
|
70
|
+
exports.mockIpV4 = mockIpV4;
|
|
71
|
+
exports.mockIpV6 = mockIpV6;
|
|
72
|
+
//# sourceMappingURL=ip.runtype.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ip.runtype.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { BaseRunType, JitFnCompiler, JitErrorsFnCompiler, RunTypeOptions, JitCode, BaseRunTypeFormat, TypeFormat } from '@mionjs/run-types';
|
|
2
|
+
import { ReflectionKind } from '@deepkit/type';
|
|
3
|
+
import { FormatParams_IP } from '@mionjs/core';
|
|
4
|
+
export declare class IPRunTypeFormat extends BaseRunTypeFormat<FormatParams_IP> {
|
|
5
|
+
static id: string;
|
|
6
|
+
kind: ReflectionKind;
|
|
7
|
+
name: string;
|
|
8
|
+
emitIsType(comp: JitFnCompiler, rt: BaseRunType): JitCode;
|
|
9
|
+
_mock(opts: RunTypeOptions, rt: BaseRunType): string;
|
|
10
|
+
emitIsTypeErrors(comp: JitErrorsFnCompiler, rt: BaseRunType): JitCode;
|
|
11
|
+
emitFormat(comp: JitFnCompiler): JitCode;
|
|
12
|
+
}
|
|
13
|
+
export declare function mockIpV4(p: FormatParams_IP): string;
|
|
14
|
+
export declare function mockIpV6(p: FormatParams_IP): string;
|
|
15
|
+
export declare const IP_RUN_TYPE_FORMATTER: IPRunTypeFormat;
|
|
16
|
+
type DEFAULT_IP_PARAMS = {
|
|
17
|
+
version: 'any';
|
|
18
|
+
allowLocalHost: true;
|
|
19
|
+
};
|
|
20
|
+
export type FormatIP<P extends FormatParams_IP = DEFAULT_IP_PARAMS> = TypeFormat<string, 'ip', P, 'ip'>;
|
|
21
|
+
export type FormatIPv4 = FormatIP<{
|
|
22
|
+
version: 4;
|
|
23
|
+
allowLocalHost: true;
|
|
24
|
+
}>;
|
|
25
|
+
export type FormatIPv6 = FormatIP<{
|
|
26
|
+
version: 6;
|
|
27
|
+
allowLocalHost: true;
|
|
28
|
+
}>;
|
|
29
|
+
export type FormatIPWithPort = FormatIP<{
|
|
30
|
+
version: 'any';
|
|
31
|
+
allowLocalHost: true;
|
|
32
|
+
allowPort: true;
|
|
33
|
+
}>;
|
|
34
|
+
export type FormatIPv4WithPort = FormatIP<{
|
|
35
|
+
version: 4;
|
|
36
|
+
allowLocalHost: true;
|
|
37
|
+
allowPort: true;
|
|
38
|
+
}>;
|
|
39
|
+
export type FormatIPv6WithPort = FormatIP<{
|
|
40
|
+
version: 6;
|
|
41
|
+
allowLocalHost: true;
|
|
42
|
+
allowPort: true;
|
|
43
|
+
}>;
|
|
44
|
+
export {};
|
|
45
|
+
export declare type __ΩFormatIP = any[];
|
|
46
|
+
export declare type __ΩFormatIPv4 = any[];
|
|
47
|
+
export declare type __ΩFormatIPv6 = any[];
|
|
48
|
+
export declare type __ΩFormatIPWithPort = any[];
|
|
49
|
+
export declare type __ΩFormatIPv4WithPort = any[];
|
|
50
|
+
export declare type __ΩFormatIPv6WithPort = any[];
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const runTypes = require("@mionjs/run-types");
|
|
4
|
+
const type = require("@deepkit/type");
|
|
5
|
+
const src_utils = require("../utils.cjs");
|
|
6
|
+
const defaultMessages = {
|
|
7
|
+
allowedChars: "Invalid characters",
|
|
8
|
+
disallowedChars: "Invalid characters",
|
|
9
|
+
allowedValues: "Invalid value",
|
|
10
|
+
disallowedValues: "Invalid value",
|
|
11
|
+
pattern: "Invalid pattern"
|
|
12
|
+
};
|
|
13
|
+
const propsWithRequiredSamples = ["disallowedChars", "disallowedChars", "pattern"];
|
|
14
|
+
const stringIgnoreProps = ["samples", "sampleChars"];
|
|
15
|
+
class StringRunTypeFormat extends runTypes.BaseRunTypeFormat {
|
|
16
|
+
static id = "stringFormat";
|
|
17
|
+
kind = type.ReflectionKind.string;
|
|
18
|
+
name = StringRunTypeFormat.id;
|
|
19
|
+
getIgnoredProps() {
|
|
20
|
+
return stringIgnoreProps;
|
|
21
|
+
}
|
|
22
|
+
emitFormat(comp, rt) {
|
|
23
|
+
const operations = [];
|
|
24
|
+
const p = this.getParams(rt);
|
|
25
|
+
const vλl = comp.vλl;
|
|
26
|
+
if (p.trim)
|
|
27
|
+
operations.push((v) => `${v}.trim()`);
|
|
28
|
+
if (p.replace)
|
|
29
|
+
operations.push((v) => `${v}.replace(${p?.replace?.searchValue}, ${p?.replace?.replaceValue})`);
|
|
30
|
+
if (p.replaceAll)
|
|
31
|
+
operations.push((v) => `${v}.replaceAll(${p?.replaceAll?.searchValue}, ${p?.replaceAll?.replaceValue})`);
|
|
32
|
+
if (p.lowercase)
|
|
33
|
+
operations.push((v) => `${v}.toLowerCase()`);
|
|
34
|
+
if (p.uppercase)
|
|
35
|
+
operations.push((v) => `${v}.toUpperCase()`);
|
|
36
|
+
if (p.capitalize)
|
|
37
|
+
operations.push((v) => `(${v}.charAt(0).toUpperCase() + ${vλl}.slice(1))`);
|
|
38
|
+
return { code: operations.reduce((acc, op) => op(acc), vλl), type: "E" };
|
|
39
|
+
}
|
|
40
|
+
emitIsType(comp, rt) {
|
|
41
|
+
const conditions = [];
|
|
42
|
+
const p = this.getParams(rt);
|
|
43
|
+
const vλl = comp.vλl;
|
|
44
|
+
const literalFn = runTypes.getToLiteralFn(comp, this.getIgnoredProps());
|
|
45
|
+
if (p.maxLength !== void 0)
|
|
46
|
+
conditions.push(`${vλl}.length <= ${literalFn(p.maxLength)}`);
|
|
47
|
+
if (p.minLength !== void 0)
|
|
48
|
+
conditions.push(`${vλl}.length >= ${literalFn(p.minLength)}`);
|
|
49
|
+
if (p.length !== void 0)
|
|
50
|
+
conditions.push(`${vλl}.length === ${literalFn(p.length)}`);
|
|
51
|
+
if (p.pattern !== void 0)
|
|
52
|
+
conditions.push(`${literalFn(p.pattern.val)}.test(${vλl})`);
|
|
53
|
+
if (p.allowedChars) {
|
|
54
|
+
const regexp = getAllowedCharsRegexp(p.allowedChars.val, p.allowedChars.ignoreCase);
|
|
55
|
+
conditions.push(`${literalFn(regexp)}.test(${vλl})`);
|
|
56
|
+
}
|
|
57
|
+
if (p.disallowedChars) {
|
|
58
|
+
const regexp = getDisallowedCharsRegexp(p.disallowedChars.val, p.disallowedChars.ignoreCase);
|
|
59
|
+
conditions.push(`!${literalFn(regexp)}.test(${vλl})`);
|
|
60
|
+
}
|
|
61
|
+
if (p.allowedValues) {
|
|
62
|
+
const regexp = getAllowedValuesRegexp(p.allowedValues.val, p.allowedValues.ignoreCase);
|
|
63
|
+
conditions.push(`${literalFn(regexp)}.test(${vλl})`);
|
|
64
|
+
}
|
|
65
|
+
if (p.disallowedValues) {
|
|
66
|
+
const regexp = getDisallowedValuesRegexp(p.disallowedValues.val, p.disallowedValues.ignoreCase);
|
|
67
|
+
conditions.push(`!${literalFn(regexp)}.test(${vλl})`);
|
|
68
|
+
}
|
|
69
|
+
return { code: conditions.join(" && "), type: "E" };
|
|
70
|
+
}
|
|
71
|
+
emitIsTypeErrors(comp, rt) {
|
|
72
|
+
const conditions = [];
|
|
73
|
+
const p = this.getParams(rt);
|
|
74
|
+
const vλl = comp.vλl;
|
|
75
|
+
const literalFn = runTypes.getToLiteralFn(comp, this.getIgnoredProps());
|
|
76
|
+
const errFn = this.getCallJitFormatErr(comp, rt, this, false);
|
|
77
|
+
if (p.maxLength !== void 0) {
|
|
78
|
+
const maxL = src_utils.paramVal(p.maxLength);
|
|
79
|
+
const errCode = errFn("maxLength", maxL);
|
|
80
|
+
conditions.push(`if (${vλl}.length > ${maxL}) ${errCode}`);
|
|
81
|
+
}
|
|
82
|
+
if (p.minLength !== void 0) {
|
|
83
|
+
const minL = src_utils.paramVal(p.minLength);
|
|
84
|
+
const errCode = errFn("minLength", minL);
|
|
85
|
+
conditions.push(`if (${vλl}.length < ${minL}) ${errCode}`);
|
|
86
|
+
}
|
|
87
|
+
if (p.length !== void 0) {
|
|
88
|
+
const length = src_utils.paramVal(p.length);
|
|
89
|
+
const errCode = errFn("length", length);
|
|
90
|
+
conditions.push(`if (${vλl}.length !== ${length}) ${errCode}`);
|
|
91
|
+
}
|
|
92
|
+
if (p.pattern !== void 0) {
|
|
93
|
+
const errCode = errFn("pattern", getDefaultMessage("pattern", p));
|
|
94
|
+
conditions.push(`if (!${literalFn(p.pattern.val)}.test(${vλl})) ${errCode}`);
|
|
95
|
+
}
|
|
96
|
+
if (p.allowedChars) {
|
|
97
|
+
const regexp = getAllowedCharsRegexp(p.allowedChars.val, p.allowedChars.ignoreCase);
|
|
98
|
+
const errCode = errFn("allowedChars", getDefaultMessage("allowedChars", p));
|
|
99
|
+
conditions.push(`if (!${literalFn(regexp)}.test(${vλl})) ${errCode}`);
|
|
100
|
+
}
|
|
101
|
+
if (p.disallowedChars) {
|
|
102
|
+
const regexp = getDisallowedCharsRegexp(p.disallowedChars.val, p.disallowedChars.ignoreCase);
|
|
103
|
+
const errCode = errFn("disallowedChars", getDefaultMessage("disallowedChars", p));
|
|
104
|
+
conditions.push(`if (${literalFn(regexp)}.test(${vλl})) ${errCode}`);
|
|
105
|
+
}
|
|
106
|
+
if (p.allowedValues) {
|
|
107
|
+
const regexp = getAllowedValuesRegexp(p.allowedValues.val, p.allowedValues.ignoreCase);
|
|
108
|
+
const errCode = errFn("allowedValues", getDefaultMessage("allowedValues", p));
|
|
109
|
+
conditions.push(`if (!${literalFn(regexp)}.test(${vλl})) ${errCode}`);
|
|
110
|
+
}
|
|
111
|
+
if (p.disallowedValues) {
|
|
112
|
+
const regexp = getDisallowedValuesRegexp(p.disallowedValues.val, p.disallowedValues.ignoreCase);
|
|
113
|
+
const errCode = errFn("disallowedValues", getDefaultMessage("disallowedValues", p));
|
|
114
|
+
conditions.push(`if (${literalFn(regexp)}.test(${vλl})) ${errCode}`);
|
|
115
|
+
}
|
|
116
|
+
return { code: conditions.join(";"), type: "S" };
|
|
117
|
+
}
|
|
118
|
+
_mock(opts, rt, params) {
|
|
119
|
+
const p = params || this.getParams(rt);
|
|
120
|
+
if (p.allowedValues)
|
|
121
|
+
return runTypes.randomItem(p.allowedValues.val);
|
|
122
|
+
const samples = p.pattern?.mockSamples || p.disallowedChars?.mockSamples || p.disallowedValues?.mockSamples;
|
|
123
|
+
if (samples && typeof samples !== "string" && Array.isArray(samples)) {
|
|
124
|
+
const ignoreCase = p.disallowedChars?.ignoreCase || p.disallowedValues?.ignoreCase;
|
|
125
|
+
const samplesAllCases = ignoreCase ? valuesAllCases(samples) : samples;
|
|
126
|
+
return runTypes.randomItem(samplesAllCases);
|
|
127
|
+
}
|
|
128
|
+
if (samples && typeof samples === "string") {
|
|
129
|
+
return this.mockString(p, samples, void 0);
|
|
130
|
+
}
|
|
131
|
+
const allowedChars = p.allowedChars?.ignoreCase ? charsAllCases(p.allowedChars.val) : p.allowedChars?.val;
|
|
132
|
+
const disallowedChars = p.disallowedChars?.ignoreCase ? charsAllCases(p.disallowedChars.val) : p.disallowedChars?.val;
|
|
133
|
+
return this.mockString(p, allowedChars, disallowedChars);
|
|
134
|
+
}
|
|
135
|
+
mockString(p, allowedChars, disallowedChars) {
|
|
136
|
+
switch (true) {
|
|
137
|
+
case p.length !== void 0:
|
|
138
|
+
return runTypes.mockString(src_utils.paramVal(p.length), allowedChars, disallowedChars);
|
|
139
|
+
case (p.maxLength !== void 0 && p.minLength !== void 0):
|
|
140
|
+
return runTypes.mockString(runTypes.random(src_utils.paramVal(p.minLength), src_utils.paramVal(p.maxLength)), allowedChars, disallowedChars);
|
|
141
|
+
case p.maxLength !== void 0:
|
|
142
|
+
return runTypes.mockString(runTypes.random(0, src_utils.paramVal(p.maxLength)), allowedChars, disallowedChars);
|
|
143
|
+
case p.minLength !== void 0: {
|
|
144
|
+
const minLength = src_utils.paramVal(p.minLength);
|
|
145
|
+
return runTypes.mockString(runTypes.random(minLength, minLength + runTypes.random(1, 1 + minLength * 2)), allowedChars, disallowedChars);
|
|
146
|
+
}
|
|
147
|
+
default:
|
|
148
|
+
return runTypes.mockString(void 0, allowedChars, disallowedChars);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
validateParams(rt, p) {
|
|
152
|
+
const throwIfInvalid = (v, name, subName, skipLengthChecks = false) => {
|
|
153
|
+
const err = getParamError(v, p, skipLengthChecks);
|
|
154
|
+
if (err)
|
|
155
|
+
throw new Error(`Parameter ${name}.${subName} "${v}" does not satisfies "${err}" in type ${this.printPath(rt, subName)}`);
|
|
156
|
+
};
|
|
157
|
+
if (p.length !== void 0 && (p.maxLength !== void 0 || p.minLength !== void 0))
|
|
158
|
+
throw new Error(`length can not be used with maxLength or minLength in ${this.printPath(rt, "length")}`);
|
|
159
|
+
if (p.maxLength !== void 0 && p.minLength !== void 0 && p.maxLength < p.minLength)
|
|
160
|
+
throw new Error(`maxLength can not be less than minLength in ${this.printPath(rt, "maxLength")}`);
|
|
161
|
+
if (p.allowedValues?.val && p.allowedValues?.val.length > 100)
|
|
162
|
+
throw new Error(`allowedValues can not have more than 100 values in ${this.printPath(rt, "allowedValues")}`);
|
|
163
|
+
if (p.disallowedValues?.val && p.disallowedValues?.val.length > 100)
|
|
164
|
+
throw new Error(`disallowedValues can not have more than 100 values in ${this.printPath(rt, "disallowedValues")}`);
|
|
165
|
+
const complexParams = [
|
|
166
|
+
{ name: "pattern", param: p.pattern },
|
|
167
|
+
{ name: "allowedChars", param: p.allowedChars },
|
|
168
|
+
{ name: "disallowedChars", param: p.disallowedChars },
|
|
169
|
+
{ name: "allowedValues", param: p.allowedValues },
|
|
170
|
+
{ name: "disallowedValues", param: p.disallowedValues }
|
|
171
|
+
].filter((p2) => p2.param !== void 0);
|
|
172
|
+
if (complexParams.length > 1) {
|
|
173
|
+
throw new Error(`Only one of the parameters [pattern, allowedChars, disallowedChars, allowedValues, disallowedValues] can be used at once in ${this.printPath(rt)}`);
|
|
174
|
+
}
|
|
175
|
+
complexParams.forEach((c) => {
|
|
176
|
+
const { mockSamples } = c.param;
|
|
177
|
+
const requireSamples = propsWithRequiredSamples.includes(c.name);
|
|
178
|
+
if (requireSamples && !mockSamples)
|
|
179
|
+
throw new Error(`When ${c.name} is defined it is also required to provide samples in ${this.printPath(rt, c.name)}`);
|
|
180
|
+
if (Array.isArray(mockSamples) && mockSamples.length === 0)
|
|
181
|
+
throw new Error(`${c.name}.mockSamples can not be an empty array in ${this.printPath(rt, "mockSamples")}`);
|
|
182
|
+
if (typeof mockSamples === "string" && mockSamples.length === 0)
|
|
183
|
+
throw new Error(`${c.name}.mockSamples can not be an empty string in ${this.printPath(rt, "mockSamples")}`);
|
|
184
|
+
if (Array.isArray(mockSamples))
|
|
185
|
+
mockSamples.forEach((sample) => throwIfInvalid(sample, c.name, "mockSamples"));
|
|
186
|
+
if (typeof mockSamples === "string")
|
|
187
|
+
mockSamples.split("").forEach((char) => throwIfInvalid(char, c.name, "mockSamples", true));
|
|
188
|
+
});
|
|
189
|
+
if (p.allowedChars?.val)
|
|
190
|
+
throwIfInvalid(p.allowedChars?.val, "allowedChars", "val", true);
|
|
191
|
+
p.allowedValues?.val.forEach((v) => throwIfInvalid(v, "allowedValues", "val"));
|
|
192
|
+
if (p.pattern && p.replace && !p.pattern.val.test(p.replace.searchValue))
|
|
193
|
+
throw new Error(`replace.searchValue "${p.replace.searchValue}" invalid for pattern ${p.pattern.val} in ${this.printPath(rt, "replace")}`);
|
|
194
|
+
if (p.pattern && p.replaceAll && !p.pattern.val.test(p.replaceAll.searchValue))
|
|
195
|
+
throw new Error(`replaceAll.searchValue "${p.replaceAll.searchValue}" invalid for pattern ${p.pattern.val} in ${this.printPath(rt, "replaceAll")}`);
|
|
196
|
+
if ([p.lowercase, p.uppercase, p.capitalize].filter(Boolean).length > 1) {
|
|
197
|
+
throw new Error(`Only one text formatter (lowercase, uppercase, capitalize) allowed for ${this.printPath(rt, "formatters")}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function isPatternParam(p) {
|
|
202
|
+
const isMessage = p.message === void 0 || typeof p.message === "string";
|
|
203
|
+
const isSamples = p.samples === void 0 || Array.isArray(p.samples) && p.samples.every((s) => typeof s === "string");
|
|
204
|
+
const isSampleChars = p.sampleChars === void 0 || typeof p.sampleChars === "string" && p.sampleChars.length > 0;
|
|
205
|
+
const hasSamples = !!p.samples?.length || !!p.sampleChars?.length;
|
|
206
|
+
const isRegexpObj = typeof p.regexp === "object" && p.regexp instanceof RegExp;
|
|
207
|
+
return isMessage && hasSamples && isSamples && isSampleChars && isRegexpObj;
|
|
208
|
+
}
|
|
209
|
+
function patternParamsToStrParams(p) {
|
|
210
|
+
return { pattern: p };
|
|
211
|
+
}
|
|
212
|
+
function getParamError(v, p, skipLengthChecks = false) {
|
|
213
|
+
if (!skipLengthChecks) {
|
|
214
|
+
if (p.maxLength !== void 0 && v.length > p.maxLength)
|
|
215
|
+
return "maxLength";
|
|
216
|
+
if (p.minLength !== void 0 && v.length < p.minLength)
|
|
217
|
+
return "minLength";
|
|
218
|
+
if (p.length !== void 0 && v.length !== p.length)
|
|
219
|
+
return "length";
|
|
220
|
+
}
|
|
221
|
+
if (p.pattern && !p.pattern.val.test(v))
|
|
222
|
+
return "pattern";
|
|
223
|
+
if (p.allowedChars && !getAllowedCharsRegexp(p.allowedChars.val).test(v))
|
|
224
|
+
return "allowedChars";
|
|
225
|
+
if (p.disallowedChars && getDisallowedCharsRegexp(p.disallowedChars.val).test(v))
|
|
226
|
+
return "disallowedChars";
|
|
227
|
+
if (p.allowedValues && !p.allowedValues.val.includes(v))
|
|
228
|
+
return "allowedValues";
|
|
229
|
+
if (p.disallowedValues && p.disallowedValues.val.includes(v))
|
|
230
|
+
return "disallowedValues";
|
|
231
|
+
}
|
|
232
|
+
function getDefaultMessage(name, p) {
|
|
233
|
+
return p[name]?.errorMessage || defaultMessages[name];
|
|
234
|
+
}
|
|
235
|
+
function getAllowedCharsRegexp(allowedChars, ignoreCase) {
|
|
236
|
+
const flags = ignoreCase ? "i" : "";
|
|
237
|
+
return new RegExp(`^[${src_utils.regexpEscape(allowedChars)}]+$`, flags);
|
|
238
|
+
}
|
|
239
|
+
function getDisallowedCharsRegexp(disallowedChars, ignoreCase) {
|
|
240
|
+
const flags = ignoreCase ? "i" : "";
|
|
241
|
+
return new RegExp(`[${src_utils.regexpEscape(disallowedChars)}]`, flags);
|
|
242
|
+
}
|
|
243
|
+
function getAllowedValuesRegexp(allowedValues, ignoreCase) {
|
|
244
|
+
const flags = ignoreCase ? "i" : "";
|
|
245
|
+
return new RegExp(`^(?:${allowedValues.map((v) => src_utils.regexpEscape(v)).join("|")})$`, flags);
|
|
246
|
+
}
|
|
247
|
+
function getDisallowedValuesRegexp(disallowedValues, ignoreCase) {
|
|
248
|
+
const flags = ignoreCase ? "i" : "";
|
|
249
|
+
return new RegExp(`^(?:${disallowedValues.map((v) => src_utils.regexpEscape(v)).join("|")})$`, flags);
|
|
250
|
+
}
|
|
251
|
+
function charsAllCases(string) {
|
|
252
|
+
if (!string)
|
|
253
|
+
return;
|
|
254
|
+
const chars = string.split("");
|
|
255
|
+
const allCases = [...chars.map((c) => c.toLowerCase()), ...chars.map((c) => c.toUpperCase())];
|
|
256
|
+
return Array.from(new Set(allCases)).join("");
|
|
257
|
+
}
|
|
258
|
+
function valuesAllCases(stringList) {
|
|
259
|
+
const allCases = [...stringList, ...stringList.map((s) => s.toLowerCase()), ...stringList.map((s) => s.toUpperCase())];
|
|
260
|
+
return Array.from(new Set(allCases));
|
|
261
|
+
}
|
|
262
|
+
const STRING_RUN_TYPE_FORMATTER = runTypes.registerFormatter(new StringRunTypeFormat());
|
|
263
|
+
const __ΩFormatString = ["P", "BrandName", () => runTypes.__ΩTypeFormat, () => StringRunTypeFormat.id, "FormatString", 'PMc!!c"&i$e!!e!"o#%w%y'];
|
|
264
|
+
exports.STRING_RUN_TYPE_FORMATTER = STRING_RUN_TYPE_FORMATTER;
|
|
265
|
+
exports.StringRunTypeFormat = StringRunTypeFormat;
|
|
266
|
+
exports.__ΩFormatString = __ΩFormatString;
|
|
267
|
+
exports.isPatternParam = isPatternParam;
|
|
268
|
+
exports.patternParamsToStrParams = patternParamsToStrParams;
|
|
269
|
+
exports.stringIgnoreProps = stringIgnoreProps;
|
|
270
|
+
//# sourceMappingURL=stringFormat.runtype.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stringFormat.runtype.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { BaseRunType, JitFnCompiler, JitErrorsFnCompiler, JitCode, TypeFormat, BaseRunTypeFormat, RunTypeOptions } from '@mionjs/run-types';
|
|
2
|
+
import { ReflectionKind } from '@deepkit/type';
|
|
3
|
+
import { FormatParam_Pattern, StringParams } from '@mionjs/core';
|
|
4
|
+
export declare const stringIgnoreProps: string[];
|
|
5
|
+
export declare class StringRunTypeFormat extends BaseRunTypeFormat<StringParams> {
|
|
6
|
+
static readonly id: "stringFormat";
|
|
7
|
+
readonly kind = ReflectionKind.string;
|
|
8
|
+
readonly name: "stringFormat";
|
|
9
|
+
getIgnoredProps(): string[] | undefined;
|
|
10
|
+
emitFormat(comp: JitFnCompiler, rt: BaseRunType): JitCode;
|
|
11
|
+
emitIsType(comp: JitFnCompiler, rt: BaseRunType): JitCode;
|
|
12
|
+
emitIsTypeErrors(comp: JitErrorsFnCompiler, rt: BaseRunType): JitCode;
|
|
13
|
+
_mock(opts: RunTypeOptions, rt: BaseRunType, params?: StringParams): string;
|
|
14
|
+
private mockString;
|
|
15
|
+
validateParams(rt: BaseRunType, p: StringParams): void;
|
|
16
|
+
}
|
|
17
|
+
export declare function isPatternParam(p: any): p is FormatParam_Pattern;
|
|
18
|
+
export declare function patternParamsToStrParams(p: FormatParam_Pattern): StringParams;
|
|
19
|
+
export declare const STRING_RUN_TYPE_FORMATTER: StringRunTypeFormat;
|
|
20
|
+
export type FormatString<P extends StringParams = {}, BrandName extends string = never> = TypeFormat<string, typeof StringRunTypeFormat.id, P, BrandName>;
|
|
21
|
+
export declare type __ΩFormatString = any[];
|