@basictech/schema 0.1.0-beta.0 → 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.
@@ -1,22 +1,23 @@
1
1
 
2
2
  
3
- > @basictech/schema@0.0.1 build
3
+ > @basictech/schema@0.1.0-beta.1 build
4
4
  > tsup
5
5
 
6
6
  CLI Building entry: ./index.ts
7
+ CLI Using tsconfig: tsconfig.json
7
8
  CLI tsup v7.2.0
8
9
  CLI Using tsup config: /Users/raz/codebook/basic/libs/client-ts/packages/schema/tsup.config.ts
9
- CLI Target: node16
10
+ CLI Target: es2022
10
11
  CLI Cleaning output folder
11
12
  CJS Build start
12
13
  ESM Build start
13
- ESM dist/index.mjs 153.00 B
14
- ESM dist/index.mjs.map 210.00 B
15
- ESM ⚡️ Build success in 6ms
16
- CJS dist/index.js 1.01 KB
17
- CJS dist/index.js.map 253.00 B
18
- CJS ⚡️ Build success in 6ms
14
+ ESM dist/index.mjs 8.36 KB
15
+ ESM dist/index.mjs.map 19.00 KB
16
+ ESM ⚡️ Build success in 8ms
17
+ CJS dist/index.js 10.11 KB
18
+ CJS dist/index.js.map 19.08 KB
19
+ CJS ⚡️ Build success in 8ms
19
20
  DTS Build start
20
- DTS ⚡️ Build success in 747ms
21
- DTS dist/index.d.ts 64.00 B
22
- DTS dist/index.d.mts 64.00 B
21
+ DTS ⚡️ Build success in 751ms
22
+ DTS dist/index.d.ts 2.67 KB
23
+ DTS dist/index.d.mts 2.67 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @basictech/schema
2
2
 
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 96e0156: update schema validation
8
+ - ef528e9: added schema package - test
9
+
10
+ ## 0.1.0-beta.1
11
+
12
+ ### Minor Changes
13
+
14
+ - update schema validation
15
+
3
16
  ## 0.1.0-beta.0
4
17
 
5
18
  ### Minor Changes
package/README.md ADDED
@@ -0,0 +1,161 @@
1
+ # Basic Schema Library
2
+
3
+ A TypeScript library for validating and managing Basic schemas and their data.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @basictech/schema
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { validateSchema, validateData, generateEmptySchema, validateUpdateSchema } from '@basictech/schema'
15
+ ```
16
+
17
+ ## API Reference
18
+
19
+ ### `validateSchema(schema: any)`
20
+
21
+ Validates if a schema follows the Basic schema format.
22
+
23
+ **Parameters:**
24
+ - `schema` (required): The schema object to validate
25
+
26
+ ```typescript
27
+ const result = validateSchema(mySchema)
28
+ // Returns: { valid: boolean, errors: ErrorObject[] }
29
+ ```
30
+
31
+ ### `validateData(schema: any, table: string, data: Record<string, any>, checkRequired?: boolean)`
32
+
33
+ Validates data against a schema's table definition.
34
+
35
+ **Parameters:**
36
+ - `schema` (required): The schema to validate against
37
+ - `table` (required): The table name within the schema
38
+ - `data` (required): The data object to validate
39
+ - `checkRequired` (optional): Whether to validate required fields (defaults to true)
40
+
41
+ ```typescript
42
+ const data = {
43
+ name: "John",
44
+ age: 30
45
+ }
46
+
47
+ const result = validateData(mySchema, "users", data)
48
+ // Returns: { valid: boolean, errors: any[], message?: string }
49
+ ```
50
+
51
+ ### `generateEmptySchema(project_id?: string, version?: number)`
52
+
53
+ Creates a new empty schema with basic structure.
54
+
55
+ **Parameters:**
56
+ - `project_id` (optional): The project identifier (defaults to "")
57
+ - `version` (optional): The schema version number (defaults to 0)
58
+
59
+ ```typescript
60
+ const newSchema = generateEmptySchema("my-project", 1)
61
+ ```
62
+
63
+ ### `validateUpdateSchema(oldSchema: any, newSchema: any)`
64
+
65
+ Validates schema updates by comparing an old schema with a new one.
66
+
67
+ **Parameters:**
68
+ - `oldSchema` (required): The original schema
69
+ - `newSchema` (required): The updated schema to validate
70
+
71
+ ```typescript
72
+ const result = validateUpdateSchema(oldSchema, newSchema)
73
+ // Returns: { valid: boolean, errors?: any[], changes?: SchemaChange[], message?: string }
74
+ ```
75
+
76
+ ## Schema Structure
77
+
78
+ A valid Basic schema follows this structure:
79
+
80
+ ```typescript
81
+ {
82
+ project_id: string,
83
+ namespace?: string,
84
+ version: number,
85
+ tables: {
86
+ [tableName: string]: {
87
+ name?: string,
88
+ type?: "collection",
89
+ fields: {
90
+ [fieldName: string]: {
91
+ type: "string" | "boolean" | "number" | "json",
92
+ indexed?: boolean,
93
+ required?: boolean
94
+ }
95
+ }
96
+ }
97
+ }
98
+ }
99
+ ```
100
+
101
+ ## Schema Change Types
102
+
103
+ When validating schema updates, the following change types are detected:
104
+
105
+ - `property_changed`: Changes to top-level properties
106
+ - `property_removed`: Removal of top-level properties
107
+ - `table_added`: New table added
108
+ - `table_removed`: Existing table removed
109
+ - `field_added`: New field added to a table
110
+ - `field_removed`: Field removed from a table
111
+ - `field_type_changed`: Field type modification (not allowed)
112
+ - `field_required_changed`: Required flag modification
113
+ - `field_property_added`: New field property added
114
+ - `field_property_changed`: Field property modified
115
+ - `field_property_removed`: Field property removed
116
+
117
+ ## Example
118
+
119
+ ```typescript
120
+ import { validateSchema, validateData, generateEmptySchema } from '@basictech/schema'
121
+
122
+ // Create a new schema
123
+ const mySchema = generateEmptySchema("my-project", 1)
124
+
125
+ // Add a table with fields
126
+ mySchema.tables.users = {
127
+ fields: {
128
+ name: { type: "string", required: true },
129
+ age: { type: "number" },
130
+ isActive: { type: "boolean" },
131
+ metadata: { type: "json" }
132
+ }
133
+ }
134
+
135
+ // Validate the schema
136
+ const validation = validateSchema(mySchema)
137
+ if (validation.valid) {
138
+ console.log("Schema is valid!")
139
+ } else {
140
+ console.error("Schema validation errors:", validation.errors)
141
+ }
142
+
143
+ // Validate some data
144
+ const userData = {
145
+ name: "John Doe",
146
+ age: 30,
147
+ isActive: true,
148
+ metadata: { joined: "2024-03-20" }
149
+ }
150
+
151
+ const dataValidation = validateData(mySchema, "users", userData)
152
+ if (dataValidation.valid) {
153
+ console.log("Data is valid!")
154
+ } else {
155
+ console.error("Data validation error:", dataValidation.message)
156
+ }
157
+ ```
158
+
159
+ ## License
160
+
161
+ MIT
package/dist/index.d.mts CHANGED
@@ -1,3 +1,86 @@
1
- declare function hello(): string;
1
+ import { ErrorObject } from 'ajv';
2
2
 
3
- export { hello as default };
3
+ declare function generateEmptySchema(project_id?: string, version?: number): {
4
+ project_id: string;
5
+ version: number;
6
+ tables: {
7
+ fields: {
8
+ example: {
9
+ type: string;
10
+ };
11
+ };
12
+ };
13
+ };
14
+ /**
15
+ * Validate a schema
16
+ * only checks if the schema is formatted correctly, not if can be published
17
+ * @param schema - The schema to validate
18
+ * @returns {valid: boolean, errors: any[]} - The validation result
19
+ */
20
+ declare function validateSchema(schema: any): {
21
+ valid: boolean;
22
+ errors: ErrorObject[];
23
+ };
24
+ /**
25
+ * Validate data against a schema's table definition. Only checks against provided schema.
26
+ * @param schema - The schema to validate against
27
+ * @param table - The table name in the schema to validate against
28
+ * @param data - The data object to validate
29
+ * @param checkRequired - Whether to check if required fields are present (default: true)
30
+ * @returns {Object} Validation result containing:
31
+ * - valid: boolean indicating if validation passed
32
+ * - errors: Array of validation error objects
33
+ * - message: Error message if validation failed
34
+ */
35
+ declare function validateData(schema: any, table: string, data: Record<string, any>, checkRequired?: boolean): {
36
+ valid: boolean;
37
+ errors: ErrorObject<string, Record<string, any>, unknown>[];
38
+ message: string;
39
+ } | {
40
+ valid: boolean;
41
+ errors: {
42
+ message: string;
43
+ }[];
44
+ message: string;
45
+ } | {
46
+ valid: boolean;
47
+ errors: never[];
48
+ message?: undefined;
49
+ };
50
+ type SchemaChangeType = "property_changed" | "property_removed" | "table_added" | "table_removed" | "field_added" | "field_removed" | "field_type_changed" | "field_required_changed" | "field_property_added" | "field_property_changed" | "field_property_removed";
51
+ type SchemaChange = {
52
+ type: SchemaChangeType;
53
+ property?: string;
54
+ table?: string;
55
+ field?: string;
56
+ old?: any;
57
+ new?: any;
58
+ };
59
+ declare function validateUpdateSchema(oldSchema: any, newSchema: any): {
60
+ valid: boolean;
61
+ errors: ErrorObject<string, Record<string, any>, unknown>[];
62
+ message: string;
63
+ changes?: undefined;
64
+ } | {
65
+ valid: boolean;
66
+ errors: {
67
+ change: SchemaChange;
68
+ message: string;
69
+ }[];
70
+ message: string;
71
+ changes?: undefined;
72
+ } | {
73
+ valid: boolean;
74
+ changes: SchemaChange[];
75
+ errors?: undefined;
76
+ message?: undefined;
77
+ };
78
+
79
+ declare const schema: {
80
+ validateSchema: typeof validateSchema;
81
+ validateData: typeof validateData;
82
+ generateEmptySchema: typeof generateEmptySchema;
83
+ validateUpdateSchema: typeof validateUpdateSchema;
84
+ };
85
+
86
+ export { schema as default, generateEmptySchema, validateData, validateSchema, validateUpdateSchema };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,86 @@
1
- declare function hello(): string;
1
+ import { ErrorObject } from 'ajv';
2
2
 
3
- export { hello as default };
3
+ declare function generateEmptySchema(project_id?: string, version?: number): {
4
+ project_id: string;
5
+ version: number;
6
+ tables: {
7
+ fields: {
8
+ example: {
9
+ type: string;
10
+ };
11
+ };
12
+ };
13
+ };
14
+ /**
15
+ * Validate a schema
16
+ * only checks if the schema is formatted correctly, not if can be published
17
+ * @param schema - The schema to validate
18
+ * @returns {valid: boolean, errors: any[]} - The validation result
19
+ */
20
+ declare function validateSchema(schema: any): {
21
+ valid: boolean;
22
+ errors: ErrorObject[];
23
+ };
24
+ /**
25
+ * Validate data against a schema's table definition. Only checks against provided schema.
26
+ * @param schema - The schema to validate against
27
+ * @param table - The table name in the schema to validate against
28
+ * @param data - The data object to validate
29
+ * @param checkRequired - Whether to check if required fields are present (default: true)
30
+ * @returns {Object} Validation result containing:
31
+ * - valid: boolean indicating if validation passed
32
+ * - errors: Array of validation error objects
33
+ * - message: Error message if validation failed
34
+ */
35
+ declare function validateData(schema: any, table: string, data: Record<string, any>, checkRequired?: boolean): {
36
+ valid: boolean;
37
+ errors: ErrorObject<string, Record<string, any>, unknown>[];
38
+ message: string;
39
+ } | {
40
+ valid: boolean;
41
+ errors: {
42
+ message: string;
43
+ }[];
44
+ message: string;
45
+ } | {
46
+ valid: boolean;
47
+ errors: never[];
48
+ message?: undefined;
49
+ };
50
+ type SchemaChangeType = "property_changed" | "property_removed" | "table_added" | "table_removed" | "field_added" | "field_removed" | "field_type_changed" | "field_required_changed" | "field_property_added" | "field_property_changed" | "field_property_removed";
51
+ type SchemaChange = {
52
+ type: SchemaChangeType;
53
+ property?: string;
54
+ table?: string;
55
+ field?: string;
56
+ old?: any;
57
+ new?: any;
58
+ };
59
+ declare function validateUpdateSchema(oldSchema: any, newSchema: any): {
60
+ valid: boolean;
61
+ errors: ErrorObject<string, Record<string, any>, unknown>[];
62
+ message: string;
63
+ changes?: undefined;
64
+ } | {
65
+ valid: boolean;
66
+ errors: {
67
+ change: SchemaChange;
68
+ message: string;
69
+ }[];
70
+ message: string;
71
+ changes?: undefined;
72
+ } | {
73
+ valid: boolean;
74
+ changes: SchemaChange[];
75
+ errors?: undefined;
76
+ message?: undefined;
77
+ };
78
+
79
+ declare const schema: {
80
+ validateSchema: typeof validateSchema;
81
+ validateData: typeof validateData;
82
+ generateEmptySchema: typeof generateEmptySchema;
83
+ validateUpdateSchema: typeof validateUpdateSchema;
84
+ };
85
+
86
+ export { schema as default, generateEmptySchema, validateData, validateSchema, validateUpdateSchema };