@basictech/schema 0.1.0-beta.1 → 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/.turbo/turbo-build.log +3 -3
- package/CHANGELOG.md +7 -0
- package/README.md +161 -0
- package/package.json +1 -1
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @basictech/schema@0.1.0-beta.
|
|
3
|
+
> @basictech/schema@0.1.0-beta.1 build
|
|
4
4
|
> tsup
|
|
5
5
|
|
|
6
6
|
[34mCLI[39m Building entry: ./index.ts
|
|
@@ -13,11 +13,11 @@
|
|
|
13
13
|
[34mESM[39m Build start
|
|
14
14
|
[32mESM[39m [1mdist/index.mjs [22m[32m8.36 KB[39m
|
|
15
15
|
[32mESM[39m [1mdist/index.mjs.map [22m[32m19.00 KB[39m
|
|
16
|
-
[32mESM[39m ⚡️ Build success in
|
|
16
|
+
[32mESM[39m ⚡️ Build success in 8ms
|
|
17
17
|
[32mCJS[39m [1mdist/index.js [22m[32m10.11 KB[39m
|
|
18
18
|
[32mCJS[39m [1mdist/index.js.map [22m[32m19.08 KB[39m
|
|
19
19
|
[32mCJS[39m ⚡️ Build success in 8ms
|
|
20
20
|
[34mDTS[39m Build start
|
|
21
|
-
[32mDTS[39m ⚡️ Build success in
|
|
21
|
+
[32mDTS[39m ⚡️ Build success in 751ms
|
|
22
22
|
[32mDTS[39m [1mdist/index.d.ts [22m[32m2.67 KB[39m
|
|
23
23
|
[32mDTS[39m [1mdist/index.d.mts [22m[32m2.67 KB[39m
|
package/CHANGELOG.md
CHANGED
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
|