@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.
@@ -1,6 +1,6 @@
1
1
 
2
2
  
3
- > @basictech/schema@0.1.0-beta.0 build
3
+ > @basictech/schema@0.1.0-beta.1 build
4
4
  > tsup
5
5
 
6
6
  CLI Building entry: ./index.ts
@@ -13,11 +13,11 @@
13
13
  ESM Build start
14
14
  ESM dist/index.mjs 8.36 KB
15
15
  ESM dist/index.mjs.map 19.00 KB
16
- ESM ⚡️ Build success in 7ms
16
+ ESM ⚡️ Build success in 8ms
17
17
  CJS dist/index.js 10.11 KB
18
18
  CJS dist/index.js.map 19.08 KB
19
19
  CJS ⚡️ Build success in 8ms
20
20
  DTS Build start
21
- DTS ⚡️ Build success in 786ms
21
+ DTS ⚡️ Build success in 751ms
22
22
  DTS dist/index.d.ts 2.67 KB
23
23
  DTS dist/index.d.mts 2.67 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
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
+
3
10
  ## 0.1.0-beta.1
4
11
 
5
12
  ### 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basictech/schema",
3
- "version": "0.1.0-beta.1",
3
+ "version": "0.1.0",
4
4
  "description": "utils for Basic Schema",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",