@basictech/schema 0.6.0 → 0.7.0-beta.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/CHANGELOG.md +6 -0
- package/dist/index.d.mts +12 -2
- package/dist/index.d.ts +12 -2
- package/dist/index.js +603 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +601 -3
- package/dist/index.mjs.map +1 -1
- package/generated-validator.d.ts +5 -0
- package/generated-validator.js +4 -0
- package/index.test.ts +670 -0
- package/index.ts +5 -4
- package/package.json +7 -3
- package/scripts/generate-validator.js +160 -0
- package/vitest.config.ts +8 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basictech/schema",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0-beta.0",
|
|
4
4
|
"description": "utils for Basic Schema",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -10,9 +10,12 @@
|
|
|
10
10
|
"access": "public"
|
|
11
11
|
},
|
|
12
12
|
"scripts": {
|
|
13
|
+
"prebuild": "node scripts/generate-validator.js",
|
|
13
14
|
"build": "tsup",
|
|
14
15
|
"dev": "tsup --watch",
|
|
15
|
-
"test": "
|
|
16
|
+
"test": "vitest run",
|
|
17
|
+
"test:watch": "vitest",
|
|
18
|
+
"generate-validator": "node scripts/generate-validator.js"
|
|
16
19
|
},
|
|
17
20
|
"author": "",
|
|
18
21
|
"license": "ISC",
|
|
@@ -23,6 +26,7 @@
|
|
|
23
26
|
"devDependencies": {
|
|
24
27
|
"@repo/typescript-config": "*",
|
|
25
28
|
"tsup": "^7.2.0",
|
|
26
|
-
"typescript": "^5.0.0"
|
|
29
|
+
"typescript": "^5.0.0",
|
|
30
|
+
"vitest": "^1.6.0"
|
|
27
31
|
}
|
|
28
32
|
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const Ajv = require('ajv')
|
|
4
|
+
const standaloneCode = require('ajv/dist/standalone')
|
|
5
|
+
const { writeFileSync, mkdirSync } = require('fs')
|
|
6
|
+
const { dirname } = require('path')
|
|
7
|
+
|
|
8
|
+
// Define the same schema as in index.ts
|
|
9
|
+
const basicJsonSchema = {
|
|
10
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
11
|
+
"type": "object",
|
|
12
|
+
"properties": {
|
|
13
|
+
"project_id": {
|
|
14
|
+
"type": "string"
|
|
15
|
+
},
|
|
16
|
+
"namespace": {
|
|
17
|
+
"type": "string",
|
|
18
|
+
},
|
|
19
|
+
"version": {
|
|
20
|
+
"type": "integer",
|
|
21
|
+
"minimum": 0
|
|
22
|
+
},
|
|
23
|
+
"tables": {
|
|
24
|
+
"type": "object",
|
|
25
|
+
"propertyNames": {
|
|
26
|
+
"pattern": "^(?!id$|ID$|Id$|iD$)[a-zA-Z0-9][a-zA-Z0-9_]*$",
|
|
27
|
+
"minLength": 1,
|
|
28
|
+
"maxLength": 50,
|
|
29
|
+
"type": "string"
|
|
30
|
+
},
|
|
31
|
+
"patternProperties": {
|
|
32
|
+
"^(?!id$|ID$|Id$|iD$)[a-zA-Z0-9][a-zA-Z0-9_]*$": {
|
|
33
|
+
"type": "object",
|
|
34
|
+
"propertyNames": {
|
|
35
|
+
"pattern": "^(?!id$|ID$|Id$|iD$)[a-zA-Z0-9][a-zA-Z0-9_]*$",
|
|
36
|
+
"minLength": 1,
|
|
37
|
+
"maxLength": 50,
|
|
38
|
+
"type": "string"
|
|
39
|
+
},
|
|
40
|
+
"properties": {
|
|
41
|
+
"name": {
|
|
42
|
+
"type": "string"
|
|
43
|
+
},
|
|
44
|
+
"type": {
|
|
45
|
+
"type": "string",
|
|
46
|
+
"enum": ["collection"]
|
|
47
|
+
},
|
|
48
|
+
"origin": {
|
|
49
|
+
"type": "object",
|
|
50
|
+
"properties": {
|
|
51
|
+
"type": {
|
|
52
|
+
"type": "string",
|
|
53
|
+
"enum": ["reference"]
|
|
54
|
+
},
|
|
55
|
+
"project_id": {
|
|
56
|
+
"type": "string"
|
|
57
|
+
},
|
|
58
|
+
"table": {
|
|
59
|
+
"type": "string"
|
|
60
|
+
},
|
|
61
|
+
"version": {
|
|
62
|
+
"type": "integer"
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"if": {
|
|
66
|
+
"properties": { "type": { "const": "reference" } }
|
|
67
|
+
},
|
|
68
|
+
"then": {
|
|
69
|
+
"required": ["project_id", "table"]
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"fields": {
|
|
73
|
+
"type": "object",
|
|
74
|
+
"propertyNames": {
|
|
75
|
+
"pattern": "^(?!id$|ID$|Id$|iD$)[a-zA-Z0-9][a-zA-Z0-9_]*$",
|
|
76
|
+
"minLength": 1,
|
|
77
|
+
"maxLength": 50,
|
|
78
|
+
"type": "string"
|
|
79
|
+
},
|
|
80
|
+
"patternProperties": {
|
|
81
|
+
"^(?!id$|ID$|Id$|iD$)[a-zA-Z0-9][a-zA-Z0-9_]*$": {
|
|
82
|
+
"type": "object",
|
|
83
|
+
"properties": {
|
|
84
|
+
"type": {
|
|
85
|
+
"type": "string",
|
|
86
|
+
"enum": ["string", "boolean", "number", "json"]
|
|
87
|
+
},
|
|
88
|
+
"indexed": {
|
|
89
|
+
"type": "boolean"
|
|
90
|
+
},
|
|
91
|
+
"required": {
|
|
92
|
+
"type": "boolean"
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
"required": ["type"]
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
"additionalProperties": true
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
"required": ["fields"]
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
"additionalProperties": true
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
"required": ["project_id", "version", "tables"]
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
console.log('Generating standalone AJV validator...')
|
|
111
|
+
|
|
112
|
+
// Create AJV instance with source code generation enabled
|
|
113
|
+
const ajv = new Ajv({
|
|
114
|
+
code: {
|
|
115
|
+
source: true,
|
|
116
|
+
esm: true,
|
|
117
|
+
optimize: false // Disable optimizations that might require runtime deps
|
|
118
|
+
},
|
|
119
|
+
allErrors: true,
|
|
120
|
+
strict: false, // Be more lenient to avoid runtime dependencies
|
|
121
|
+
validateFormats: false, // Disable format validation to avoid dependencies
|
|
122
|
+
addUsedSchema: false // Don't add used schemas to avoid dependencies
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
// Compile the schema
|
|
126
|
+
const validate = ajv.compile(basicJsonSchema)
|
|
127
|
+
|
|
128
|
+
// Generate standalone code
|
|
129
|
+
let moduleCode = standaloneCode(ajv, validate)
|
|
130
|
+
|
|
131
|
+
// Post-process to remove runtime dependencies
|
|
132
|
+
// Replace the ucs2length require with a native implementation
|
|
133
|
+
const ucs2LengthFunc = `
|
|
134
|
+
// Modern JavaScript Unicode string length (replaces ajv/dist/runtime/ucs2length)
|
|
135
|
+
const ucs2length = (str) => [...str].length;
|
|
136
|
+
`
|
|
137
|
+
|
|
138
|
+
// Replace the require statement with our function
|
|
139
|
+
moduleCode = moduleCode.replace(
|
|
140
|
+
/const func2 = require\("ajv\/dist\/runtime\/ucs2length"\)\.default;/g,
|
|
141
|
+
ucs2LengthFunc + 'const func2 = ucs2length;'
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
// Ensure the directory exists
|
|
145
|
+
const outputPath = 'generated-validator.js'
|
|
146
|
+
const typesPath = 'generated-validator.d.ts'
|
|
147
|
+
|
|
148
|
+
// Write the generated validator
|
|
149
|
+
writeFileSync(outputPath, moduleCode)
|
|
150
|
+
|
|
151
|
+
// Create TypeScript declaration file
|
|
152
|
+
const typeDeclaration = `export declare const validate: {
|
|
153
|
+
(data: any, options?: any): boolean;
|
|
154
|
+
errors?: any[] | null;
|
|
155
|
+
};
|
|
156
|
+
export default validate;
|
|
157
|
+
`
|
|
158
|
+
writeFileSync(typesPath, typeDeclaration)
|
|
159
|
+
|
|
160
|
+
console.log(`✅ Standalone validator generated at ${outputPath}`)
|