@offlinejs/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/index.d.ts +15 -0
- package/dist/index.js +103 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ValidationIssue, SchemaValidator, EntityRecord, ValidationResult, StorageAdapter, OfflinePlugin } from '@offlinejs/types';
|
|
2
|
+
|
|
3
|
+
declare class OfflineValidationError extends Error {
|
|
4
|
+
readonly issues: ValidationIssue[];
|
|
5
|
+
constructor(issues: ValidationIssue[]);
|
|
6
|
+
}
|
|
7
|
+
type ValidatorMap = Record<string, SchemaValidator>;
|
|
8
|
+
declare const createRequiredFieldsValidator: (fields: string[]) => SchemaValidator;
|
|
9
|
+
declare const createTypeValidator: (fieldTypes: Record<string, "string" | "number" | "boolean" | "object">) => SchemaValidator;
|
|
10
|
+
declare const composeValidators: (...validators: SchemaValidator[]) => SchemaValidator;
|
|
11
|
+
declare const createValidatedStorage: (storage: StorageAdapter, validators: ValidatorMap) => StorageAdapter;
|
|
12
|
+
declare const validationPlugin: (validators: ValidatorMap) => OfflinePlugin;
|
|
13
|
+
declare const assertValid: (validator: SchemaValidator | undefined, record: EntityRecord, collection: string) => Promise<ValidationResult>;
|
|
14
|
+
|
|
15
|
+
export { OfflineValidationError, type ValidatorMap, assertValid, composeValidators, createRequiredFieldsValidator, createTypeValidator, createValidatedStorage, validationPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { STORAGE_ADAPTER_CONTRACT_VERSION } from '@offlinejs/types';
|
|
2
|
+
import { withIndexForwarding } from '@offlinejs/utils';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
var OfflineValidationError = class extends Error {
|
|
6
|
+
issues;
|
|
7
|
+
constructor(issues) {
|
|
8
|
+
super(issues.map((issue) => issue.message).join("; ") || "Validation failed");
|
|
9
|
+
this.name = "OfflineValidationError";
|
|
10
|
+
this.issues = issues;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var createRequiredFieldsValidator = (fields) => (record) => {
|
|
14
|
+
const issues = fields.filter((field) => record[field] === void 0 || record[field] === null).map((field) => ({
|
|
15
|
+
code: "required",
|
|
16
|
+
message: `"${field}" is required`,
|
|
17
|
+
path: [field]
|
|
18
|
+
}));
|
|
19
|
+
return { issues, valid: issues.length === 0 };
|
|
20
|
+
};
|
|
21
|
+
var createTypeValidator = (fieldTypes) => (record) => {
|
|
22
|
+
const issues = [];
|
|
23
|
+
for (const [field, expectedType] of Object.entries(fieldTypes)) {
|
|
24
|
+
const value = record[field];
|
|
25
|
+
if (value === void 0 || value === null) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
const actualType = Array.isArray(value) ? "object" : typeof value;
|
|
29
|
+
if (actualType !== expectedType) {
|
|
30
|
+
issues.push({
|
|
31
|
+
code: "type",
|
|
32
|
+
message: `"${field}" must be of type ${expectedType}`,
|
|
33
|
+
path: [field]
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return { issues, valid: issues.length === 0 };
|
|
38
|
+
};
|
|
39
|
+
var composeValidators = (...validators) => async (record, collection) => {
|
|
40
|
+
const issues = [];
|
|
41
|
+
for (const validator of validators) {
|
|
42
|
+
const result = await validator(record, collection);
|
|
43
|
+
issues.push(...result.issues);
|
|
44
|
+
}
|
|
45
|
+
return { issues, valid: issues.length === 0 };
|
|
46
|
+
};
|
|
47
|
+
var createValidatedStorage = (storage, validators) => {
|
|
48
|
+
const createValidatedStore = (store) => ({
|
|
49
|
+
clear: (collection) => store.clear(collection),
|
|
50
|
+
delete: (collection, id) => store.delete(collection, id),
|
|
51
|
+
find: (collection, query) => store.find(collection, query),
|
|
52
|
+
get: (collection, id) => store.get(collection, id),
|
|
53
|
+
async set(collection, value) {
|
|
54
|
+
await assertValid(validators[collection], value, collection);
|
|
55
|
+
await store.set(collection, value);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
const wrapper = {
|
|
59
|
+
name: `${storage.name}:validated`,
|
|
60
|
+
contractVersion: STORAGE_ADAPTER_CONTRACT_VERSION,
|
|
61
|
+
...storage.capabilities ? { capabilities: storage.capabilities } : {},
|
|
62
|
+
clear: (collection) => storage.clear(collection),
|
|
63
|
+
delete: (collection, id) => storage.delete(collection, id),
|
|
64
|
+
find: (collection, query) => storage.find(collection, query),
|
|
65
|
+
get: (collection, id) => storage.get(collection, id),
|
|
66
|
+
async set(collection, value) {
|
|
67
|
+
await assertValid(validators[collection], value, collection);
|
|
68
|
+
await storage.set(collection, value);
|
|
69
|
+
},
|
|
70
|
+
transaction: (scope, run) => storage.transaction(scope, (store) => run(createValidatedStore(store))),
|
|
71
|
+
...storage.migrate ? { migrate: storage.migrate.bind(storage) } : {}
|
|
72
|
+
};
|
|
73
|
+
return withIndexForwarding(wrapper, storage);
|
|
74
|
+
};
|
|
75
|
+
var validationPlugin = (validators) => ({
|
|
76
|
+
name: "validation",
|
|
77
|
+
setup({ events }) {
|
|
78
|
+
return events.on("queue:add", async (mutation) => {
|
|
79
|
+
const validator = validators[mutation.collection];
|
|
80
|
+
if (!validator || !mutation.payload) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const result = await validator(mutation.payload, mutation.collection);
|
|
84
|
+
if (!result.valid) {
|
|
85
|
+
events.emit("error", new OfflineValidationError(result.issues));
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
var assertValid = async (validator, record, collection) => {
|
|
91
|
+
if (!validator) {
|
|
92
|
+
return { issues: [], valid: true };
|
|
93
|
+
}
|
|
94
|
+
const result = await validator(record, collection);
|
|
95
|
+
if (!result.valid) {
|
|
96
|
+
throw new OfflineValidationError(result.issues);
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export { OfflineValidationError, assertValid, composeValidators, createRequiredFieldsValidator, createTypeValidator, createValidatedStorage, validationPlugin };
|
|
102
|
+
//# sourceMappingURL=index.js.map
|
|
103
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;AAaO,IAAM,sBAAA,GAAN,cAAqC,KAAA,CAAM;AAAA,EACvC,MAAA;AAAA,EAET,YAAY,MAAA,EAA2B;AACrC,IAAA,KAAA,CAAM,MAAA,CAAO,GAAA,CAAI,CAAC,KAAA,KAAU,KAAA,CAAM,OAAO,CAAA,CAAE,IAAA,CAAK,IAAI,CAAA,IAAK,mBAAmB,CAAA;AAC5E,IAAA,IAAA,CAAK,IAAA,GAAO,wBAAA;AACZ,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AAAA,EAChB;AACF;AAIO,IAAM,6BAAA,GACX,CAAC,MAAA,KACD,CAAC,MAAA,KAAW;AACV,EAAA,MAAM,SAAS,MAAA,CACZ,MAAA,CAAO,CAAC,KAAA,KAAU,OAAO,KAAK,CAAA,KAAM,MAAA,IAAa,MAAA,CAAO,KAAK,CAAA,KAAM,IAAI,CAAA,CACvE,GAAA,CAAI,CAAC,KAAA,MAAW;AAAA,IACf,IAAA,EAAM,UAAA;AAAA,IACN,OAAA,EAAS,IAAI,KAAK,CAAA,aAAA,CAAA;AAAA,IAClB,IAAA,EAAM,CAAC,KAAK;AAAA,GACd,CAAE,CAAA;AAEJ,EAAA,OAAO,EAAE,MAAA,EAAQ,KAAA,EAAO,MAAA,CAAO,WAAW,CAAA,EAAE;AAC9C;AAEK,IAAM,mBAAA,GACX,CAAC,UAAA,KACD,CAAC,MAAA,KAAW;AACV,EAAA,MAAM,SAA4B,EAAC;AAEnC,EAAA,KAAA,MAAW,CAAC,KAAA,EAAO,YAAY,KAAK,MAAA,CAAO,OAAA,CAAQ,UAAU,CAAA,EAAG;AAC9D,IAAA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AAM,IAAI,sBAAA,CAAuB,MAAA,CAAO,MAAM,CAAA;AAAA,EAChD;AAEA,EAAA,OAAO,MAAA;AACT","file":"index.js","sourcesContent":["import {\n STORAGE_ADAPTER_CONTRACT_VERSION,\n type EntityRecord,\n type OfflinePlugin,\n type QueryOptions,\n type SchemaValidator,\n type StorageAdapter,\n type TransactionStore,\n type ValidationIssue,\n type ValidationResult\n} from \"@offlinejs/types\";\nimport { withIndexForwarding } from \"@offlinejs/utils\";\n\nexport class OfflineValidationError extends Error {\n readonly issues: ValidationIssue[];\n\n constructor(issues: ValidationIssue[]) {\n super(issues.map((issue) => issue.message).join(\"; \") || \"Validation failed\");\n this.name = \"OfflineValidationError\";\n this.issues = issues;\n }\n}\n\nexport type ValidatorMap = Record<string, SchemaValidator>;\n\nexport const createRequiredFieldsValidator =\n (fields: string[]): SchemaValidator =>\n (record) => {\n const issues = fields\n .filter((field) => record[field] === undefined || record[field] === null)\n .map((field) => ({\n code: \"required\",\n message: `\"${field}\" is required`,\n path: [field]\n }));\n\n return { issues, valid: issues.length === 0 };\n };\n\nexport const createTypeValidator =\n (fieldTypes: Record<string, \"string\" | \"number\" | \"boolean\" | \"object\">): SchemaValidator =>\n (record) => {\n const issues: ValidationIssue[] = [];\n\n for (const [field, expectedType] of Object.entries(fieldTypes)) {\n const value = record[field];\n if (value === undefined || value === null) {\n continue;\n }\n\n const actualType = Array.isArray(value) ? \"object\" : typeof value;\n if (actualType !== expectedType) {\n issues.push({\n code: \"type\",\n message: `\"${field}\" must be of type ${expectedType}`,\n path: [field]\n });\n }\n }\n\n return { issues, valid: issues.length === 0 };\n };\n\nexport const composeValidators =\n (...validators: SchemaValidator[]): SchemaValidator =>\n async (record, collection) => {\n const issues: ValidationIssue[] = [];\n\n for (const validator of validators) {\n const result = await validator(record, collection);\n issues.push(...result.issues);\n }\n\n return { issues, valid: issues.length === 0 };\n };\n\nexport const createValidatedStorage = (\n storage: StorageAdapter,\n validators: ValidatorMap\n): StorageAdapter => {\n const createValidatedStore = (store: TransactionStore): TransactionStore => ({\n clear: (collection) => store.clear(collection),\n delete: (collection, id) => store.delete(collection, id),\n find: <TRecord extends EntityRecord>(collection: string, query?: QueryOptions<TRecord>) =>\n store.find(collection, query),\n get: <TRecord extends EntityRecord>(collection: string, id: string) =>\n store.get(collection, id),\n async set(collection: string, value: EntityRecord): Promise<void> {\n await assertValid(validators[collection], value, collection);\n await store.set(collection, value);\n }\n });\n\n const wrapper: StorageAdapter = {\n name: `${storage.name}:validated`,\n contractVersion: STORAGE_ADAPTER_CONTRACT_VERSION,\n ...(storage.capabilities ? { capabilities: storage.capabilities } : {}),\n clear: (collection) => storage.clear(collection),\n delete: (collection, id) => storage.delete(collection, id),\n find: <TRecord extends EntityRecord>(\n collection: string,\n query?: QueryOptions<TRecord>\n ): Promise<TRecord[]> => storage.find(collection, query),\n get: <TRecord extends EntityRecord>(collection: string, id: string): Promise<TRecord | null> =>\n storage.get(collection, id),\n async set(collection: string, value: EntityRecord): Promise<void> {\n await assertValid(validators[collection], value, collection);\n await storage.set(collection, value);\n },\n transaction: <TValue>(scope: string[], run: (store: TransactionStore) => Promise<TValue>) =>\n storage.transaction(scope, (store) => run(createValidatedStore(store))),\n ...(storage.migrate ? { migrate: storage.migrate.bind(storage) } : {})\n };\n\n return withIndexForwarding(wrapper, storage);\n};\n\nexport const validationPlugin = (validators: ValidatorMap): OfflinePlugin => ({\n name: \"validation\",\n setup({ events }) {\n return events.on(\"queue:add\", async (mutation) => {\n const validator = validators[mutation.collection];\n\n if (!validator || !mutation.payload) {\n return;\n }\n\n const result = await validator(mutation.payload, mutation.collection);\n\n if (!result.valid) {\n events.emit(\"error\", new OfflineValidationError(result.issues));\n }\n });\n }\n});\n\nexport const assertValid = async (\n validator: SchemaValidator | undefined,\n record: EntityRecord,\n collection: string\n): Promise<ValidationResult> => {\n if (!validator) {\n return { issues: [], valid: true };\n }\n\n const result = await validator(record, collection);\n\n if (!result.valid) {\n throw new OfflineValidationError(result.issues);\n }\n\n return result;\n};\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@offlinejs/validation",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Schema validation helpers and storage wrapper for OfflineJS.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@offlinejs/utils": "0.1.0",
|
|
18
|
+
"@offlinejs/types": "0.1.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"tsup": "latest",
|
|
22
|
+
"typescript": "latest"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"registry": "https://registry.npmjs.org/"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup",
|
|
30
|
+
"typecheck": "tsc --noEmit"
|
|
31
|
+
}
|
|
32
|
+
}
|