@oktis-works/validation 0.1.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/basic.d.ts +12 -0
- package/dist/basic.d.ts.map +1 -0
- package/dist/basic.js +94 -0
- package/dist/basic.js.map +1 -0
- package/dist/choice.d.ts +11 -0
- package/dist/choice.d.ts.map +1 -0
- package/dist/choice.js +84 -0
- package/dist/choice.js.map +1 -0
- package/dist/client-validate.d.ts +17 -0
- package/dist/client-validate.d.ts.map +1 -0
- package/dist/client-validate.js +354 -0
- package/dist/client-validate.js.map +1 -0
- package/dist/clone.d.ts +16 -0
- package/dist/clone.d.ts.map +1 -0
- package/dist/clone.js +63 -0
- package/dist/clone.js.map +1 -0
- package/dist/compatibility.d.ts +23 -0
- package/dist/compatibility.d.ts.map +1 -0
- package/dist/compatibility.js +46 -0
- package/dist/compatibility.js.map +1 -0
- package/dist/compatibility.test.d.ts +2 -0
- package/dist/compatibility.test.d.ts.map +1 -0
- package/dist/compatibility.test.js +50 -0
- package/dist/compatibility.test.js.map +1 -0
- package/dist/conditional.d.ts +30 -0
- package/dist/conditional.d.ts.map +1 -0
- package/dist/conditional.js +114 -0
- package/dist/conditional.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/media.d.ts +13 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +102 -0
- package/dist/media.js.map +1 -0
- package/dist/relational.d.ts +6 -0
- package/dist/relational.d.ts.map +1 -0
- package/dist/relational.js +113 -0
- package/dist/relational.js.map +1 -0
- package/dist/structural.d.ts +26 -0
- package/dist/structural.d.ts.map +1 -0
- package/dist/structural.js +50 -0
- package/dist/structural.js.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// @oktis-works/core - Relational & Content Field Validators (ACF parity)
|
|
2
|
+
import { isEmptyValue } from './basic.js';
|
|
3
|
+
export function validateRelationshipIds(value, config, field, errors) {
|
|
4
|
+
if (isEmptyValue(value))
|
|
5
|
+
return;
|
|
6
|
+
const multiple = config['multiple'] !== false;
|
|
7
|
+
const minSelections = config['minSelections'];
|
|
8
|
+
const maxSelections = config['maxSelections'];
|
|
9
|
+
const ids = Array.isArray(value) ? value : [value];
|
|
10
|
+
for (const id of ids) {
|
|
11
|
+
if (typeof id !== 'string' && typeof id !== 'number') {
|
|
12
|
+
errors.push({ field, code: 'INVALID_ID', message: `${field} contém um identificador inválido` });
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (multiple) {
|
|
17
|
+
if (maxSelections !== undefined && ids.length > maxSelections) {
|
|
18
|
+
errors.push({
|
|
19
|
+
field,
|
|
20
|
+
code: 'MAX_SELECTIONS',
|
|
21
|
+
message: `${field} permite no máximo ${maxSelections} seleção(ões)`,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
if (minSelections !== undefined && ids.length < minSelections) {
|
|
25
|
+
errors.push({
|
|
26
|
+
field,
|
|
27
|
+
code: 'MIN_SELECTIONS',
|
|
28
|
+
message: `${field} requer no mínimo ${minSelections} seleção(ões)`,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else if (ids.length > 1) {
|
|
33
|
+
errors.push({ field, code: 'SINGLE_ONLY', message: `${field} aceita apenas um valor` });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export function validateLink(value, field, errors) {
|
|
37
|
+
if (isEmptyValue(value))
|
|
38
|
+
return;
|
|
39
|
+
if (typeof value !== 'object' || value === null) {
|
|
40
|
+
errors.push({ field, code: 'INVALID_LINK', message: `${field} deve ser um objeto de link` });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const link = value;
|
|
44
|
+
if (!link.url || typeof link.url !== 'string') {
|
|
45
|
+
errors.push({ field, code: 'LINK_URL_REQUIRED', message: `${field} requer uma URL` });
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const parsed = new URL(link.url);
|
|
50
|
+
if (!['http:', 'https:', 'mailto:', 'tel:'].includes(parsed.protocol) && !link.url.startsWith('#') && !link.url.startsWith('/')) {
|
|
51
|
+
throw new Error('protocol');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
if (!link.url.startsWith('#') && !link.url.startsWith('/') && !link.url.startsWith('?')) {
|
|
56
|
+
errors.push({ field, code: 'INVALID_URL', message: `${field} contém uma URL inválida` });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (link.target !== undefined && !['_self', '_blank'].includes(String(link.target))) {
|
|
60
|
+
errors.push({ field, code: 'INVALID_TARGET', message: `${field} aceita apenas _self ou _blank` });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export function validateGoogleMap(value, field, errors) {
|
|
64
|
+
if (isEmptyValue(value))
|
|
65
|
+
return;
|
|
66
|
+
if (typeof value !== 'object' || value === null) {
|
|
67
|
+
errors.push({ field, code: 'INVALID_MAP', message: `${field} deve conter endereço e coordenadas` });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const map = value;
|
|
71
|
+
if (isEmptyValue(map.address)) {
|
|
72
|
+
errors.push({ field, code: 'ADDRESS_REQUIRED', message: `${field} requer um endereço` });
|
|
73
|
+
}
|
|
74
|
+
const lat = Number(map.lat);
|
|
75
|
+
const lng = Number(map.lng);
|
|
76
|
+
if (!Number.isFinite(lat) || lat < -90 || lat > 90) {
|
|
77
|
+
errors.push({ field, code: 'INVALID_LATITUDE', message: `${field} contém latitude inválida (-90 a 90)` });
|
|
78
|
+
}
|
|
79
|
+
if (!Number.isFinite(lng) || lng < -180 || lng > 180) {
|
|
80
|
+
errors.push({ field, code: 'INVALID_LONGITUDE', message: `${field} contém longitude inválida (-180 a 180)` });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const HEX_PATTERN = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
|
|
84
|
+
export function validateColorPicker(value, config, field, errors) {
|
|
85
|
+
if (isEmptyValue(value))
|
|
86
|
+
return;
|
|
87
|
+
const color = String(value);
|
|
88
|
+
const isRgba = color.startsWith('rgba(');
|
|
89
|
+
if (isRgba && !config['enableOpacity']) {
|
|
90
|
+
errors.push({
|
|
91
|
+
field,
|
|
92
|
+
code: 'OPACITY_DISABLED',
|
|
93
|
+
message: `${field} não permite transparência`,
|
|
94
|
+
});
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (!isRgba && !HEX_PATTERN.test(color)) {
|
|
98
|
+
errors.push({ field, code: 'INVALID_COLOR', message: `${field} deve ser uma cor em formato hexadecimal válido` });
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const palette = config['palette'] ?? [];
|
|
102
|
+
if (palette.length > 0) {
|
|
103
|
+
const normalizedPalette = palette.map((entry) => entry.toLowerCase());
|
|
104
|
+
if (!normalizedPalette.includes(color.toLowerCase())) {
|
|
105
|
+
errors.push({
|
|
106
|
+
field,
|
|
107
|
+
code: 'NOT_IN_PALETTE',
|
|
108
|
+
message: `${field} deve usar uma cor da paleta definida`,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=relational.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relational.js","sourceRoot":"","sources":["../src/relational.ts"],"names":[],"mappings":"AAAA,yEAAyE;AAGzE,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1C,MAAM,UAAU,uBAAuB,CACrC,KAAc,EACd,MAA+B,EAC/B,KAAa,EACb,MAAoB;IAEpB,IAAI,YAAY,CAAC,KAAK,CAAC;QAAE,OAAO;IAEhC,MAAM,QAAQ,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,KAAK,CAAC;IAC9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IAClE,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,iBAAiB,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC;QACtE,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,EAAE,CAAC;YACrD,MAAM,CAAC,IAAI,CAAC;gBACV,KAAK;gBACL,IAAI,EAAE,gBAAgB;gBACtB,OAAO,EAAE,GAAG,KAAK,uCAAuC;aACzD,CAAC,CAAC;QACL,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { FieldError } from './basic.js';
|
|
2
|
+
import type { ConditionalLogic } from './conditional.js';
|
|
3
|
+
export interface ValidatableField {
|
|
4
|
+
type: string;
|
|
5
|
+
name: string;
|
|
6
|
+
label: string;
|
|
7
|
+
required?: boolean;
|
|
8
|
+
config?: Record<string, unknown>;
|
|
9
|
+
conditionalLogic?: ConditionalLogic | null;
|
|
10
|
+
subFields?: ValidatableField[];
|
|
11
|
+
layouts?: Array<{
|
|
12
|
+
name: string;
|
|
13
|
+
label: string;
|
|
14
|
+
subFields: ValidatableField[];
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
export interface FlexibleRow {
|
|
18
|
+
layout: string;
|
|
19
|
+
data: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
export declare function validateRepeater(value: unknown, config: Record<string, unknown>, field: string, errors: FieldError[]): void;
|
|
22
|
+
export declare function validateFlexible(value: unknown, layouts: Array<{
|
|
23
|
+
name: string;
|
|
24
|
+
subFields: ValidatableField[];
|
|
25
|
+
}> | undefined, config: Record<string, unknown>, field: string, errors: FieldError[]): void;
|
|
26
|
+
//# sourceMappingURL=structural.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structural.d.ts","sourceRoot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 +1,50 @@
|
|
|
1
|
+
// @oktis-works/core - Structural Field Validators (ACF parity)
|
|
2
|
+
import { isEmptyValue } from './basic.js';
|
|
3
|
+
function checkRowsCount(rows, config, field, errors) {
|
|
4
|
+
const minRows = config['minRows'];
|
|
5
|
+
const maxRows = config['maxRows'];
|
|
6
|
+
if (minRows !== undefined && rows.length < minRows) {
|
|
7
|
+
errors.push({ field, code: 'MIN_ROWS', message: `${field} requer no mínimo ${minRows} linha(s)` });
|
|
8
|
+
}
|
|
9
|
+
if (maxRows !== undefined && rows.length > maxRows) {
|
|
10
|
+
errors.push({ field, code: 'MAX_ROWS', message: `${field} permite no máximo ${maxRows} linha(s)` });
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export function validateRepeater(value, config, field, errors) {
|
|
14
|
+
if (isEmptyValue(value))
|
|
15
|
+
return;
|
|
16
|
+
if (!Array.isArray(value)) {
|
|
17
|
+
errors.push({ field, code: 'NOT_ARRAY', message: `${field} deve ser uma lista de linhas` });
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
checkRowsCount(value, config, field, errors);
|
|
21
|
+
}
|
|
22
|
+
export function validateFlexible(value, layouts, config, field, errors) {
|
|
23
|
+
if (isEmptyValue(value))
|
|
24
|
+
return;
|
|
25
|
+
if (!Array.isArray(value)) {
|
|
26
|
+
errors.push({ field, code: 'NOT_ARRAY', message: `${field} deve ser uma lista de seções` });
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
checkRowsCount(value, config, field, errors);
|
|
30
|
+
const knownLayouts = new Set((layouts ?? []).map((layout) => layout.name));
|
|
31
|
+
for (const row of value) {
|
|
32
|
+
const flexibleRow = row;
|
|
33
|
+
if (!flexibleRow || typeof flexibleRow !== 'object' || typeof flexibleRow.layout !== 'string') {
|
|
34
|
+
errors.push({
|
|
35
|
+
field,
|
|
36
|
+
code: 'INVALID_LAYOUT_ROW',
|
|
37
|
+
message: `${field} contém uma seção sem layout definido`,
|
|
38
|
+
});
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (knownLayouts.size > 0 && !knownLayouts.has(flexibleRow.layout)) {
|
|
42
|
+
errors.push({
|
|
43
|
+
field,
|
|
44
|
+
code: 'UNKNOWN_LAYOUT',
|
|
45
|
+
message: `${field} referencia o layout desconhecido "${flexibleRow.layout}"`,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=structural.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structural.js","sourceRoot":"","sources":["../src/structural.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAG/D,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAmB1C,SAAS,cAAc,CACrB,IAAe,EACf,MAA+B,EAC/B,KAAa,EACb,MAAoB;IAEpB,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,CAAuB,CAAC;IACxD,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,CAAuB,CAAC;IAExD,IAAI,OAAO,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,GAAG,OAAO,EAAE,CAAC;QACnD,MAAM,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,KAAK,qBAAqB,OAAO,WAAW,EAAE,CAAC,CAAC;IACrG,CAAC;IACD,IAAI,OAAO,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,GAAG,OAAO,EAAE,CAAC;QACnD,MAAM,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,KAAK,sBAAsB,OAAO,WAAW,EAAE,CAAC,CAAC;IACtG,CAAC;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAC9B,KAAc,EACd,MAA+B,EAC/B,KAAa,EACb,MAAoB;IAEpB,IAAI,YAAY,CAAC,KAAK,CAAC;QAAE,OAAO;IAEhC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AAI,YAAY,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC;YACnE,MAAM,CAAC,IAAI,CAAC;gBACV,KAAK;gBACL,IAAI,EAAE,gBAAgB;gBACtB,OAAO,EAAE,GAAG,KAAK,sCAAsC,WAAW,CAAC,MAAM,GAAG;aAC7E,CAAC,CAAC;QACL,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oktis-works/validation",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pure field validation library (ACF parity) — UI-safe, sem dependência de banco",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"registry": "https://registry.npmjs.org/"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./basic": {
|
|
21
|
+
"types": "./dist/basic.d.ts",
|
|
22
|
+
"import": "./dist/basic.js"
|
|
23
|
+
},
|
|
24
|
+
"./choice": {
|
|
25
|
+
"types": "./dist/choice.d.ts",
|
|
26
|
+
"import": "./dist/choice.js"
|
|
27
|
+
},
|
|
28
|
+
"./client-validate": {
|
|
29
|
+
"types": "./dist/client-validate.d.ts",
|
|
30
|
+
"import": "./dist/client-validate.js"
|
|
31
|
+
},
|
|
32
|
+
"./conditional": {
|
|
33
|
+
"types": "./dist/conditional.d.ts",
|
|
34
|
+
"import": "./dist/conditional.js"
|
|
35
|
+
},
|
|
36
|
+
"./media": {
|
|
37
|
+
"types": "./dist/media.d.ts",
|
|
38
|
+
"import": "./dist/media.js"
|
|
39
|
+
},
|
|
40
|
+
"./relational": {
|
|
41
|
+
"types": "./dist/relational.d.ts",
|
|
42
|
+
"import": "./dist/relational.js"
|
|
43
|
+
},
|
|
44
|
+
"./structural": {
|
|
45
|
+
"types": "./dist/structural.d.ts",
|
|
46
|
+
"import": "./dist/structural.js"
|
|
47
|
+
},
|
|
48
|
+
"./clone": {
|
|
49
|
+
"types": "./dist/clone.d.ts",
|
|
50
|
+
"import": "./dist/clone.js"
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsc",
|
|
55
|
+
"dev": "tsc --watch",
|
|
56
|
+
"clean": "rm -rf dist tsconfig.tsbuildinfo"
|
|
57
|
+
},
|
|
58
|
+
"license": "MIT",
|
|
59
|
+
"repository": {
|
|
60
|
+
"type": "git",
|
|
61
|
+
"url": "https://github.com/blacklotus/blacklotus-cms",
|
|
62
|
+
"directory": "packages/validation"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"semver": "7"
|
|
66
|
+
}
|
|
67
|
+
}
|