Skip to content

Commit 11f84c9

Browse files
authored
feat: add shorthand notation example to playground (#65)
Add a new "shorthand" example tab that demonstrates using type strings directly (e.g., schema('string')) instead of full object notation. Also update extractSchemaFromCode to parse shorthand syntax.
1 parent 35a0c74 commit 11f84c9

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

docs/index.html

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ <h2>playground</h2>
516516

517517
<div class="panel-tabs" style="margin-bottom: 16px;">
518518
<button class="panel-tab active" data-example="user">user object</button>
519+
<button class="panel-tab" data-example="shorthand">shorthand</button>
519520
<button class="panel-tab" data-example="union">discriminated union</button>
520521
<button class="panel-tab" data-example="recursive">recursive</button>
521522
<button class="panel-tab" data-example="formats">formats</button>
@@ -968,6 +969,24 @@ <h3 style="font-size: 14px; color: var(--text); margin-top: 32px; margin-bottom:
968969
age: 28
969970
}
970971
},
972+
shorthand: {
973+
schema: `// Shorthand: use type strings directly
974+
// instead of { type: 'string' }
975+
976+
// Simple primitive validation
977+
schema('string')
978+
979+
// Also works in properties:
980+
// schema({
981+
// type: 'object',
982+
// properties: {
983+
// name: 'string',
984+
// age: 'integer',
985+
// active: 'boolean'
986+
// }
987+
// })`,
988+
data: "hello world"
989+
},
971990
union: {
972991
schema: `schema({
973992
oneOf: [
@@ -1062,18 +1081,23 @@ <h3 style="font-size: 14px; color: var(--text); margin-top: 32px; margin-bottom:
10621081
const statusBadge = document.getElementById('status-badge');
10631082
const tabs = document.querySelectorAll('.panel-tab');
10641083

1065-
// Extract schema object from TypeScript code like "schema({...})"
1084+
// Extract schema object from TypeScript code like "schema({...})" or "schema('string')"
10661085
function extractSchemaFromCode(code) {
1067-
const match = code.match(/schema\s*\(\s*(\{[\s\S]*\})\s*\)/);
1068-
if (!match) {
1069-
throw new Error('Could not find schema({...}) in code');
1086+
// Try object literal first: schema({...})
1087+
const objectMatch = code.match(/schema\s*\(\s*(\{[\s\S]*\})\s*\)/);
1088+
if (objectMatch) {
1089+
try {
1090+
return new Function('return ' + objectMatch[1])();
1091+
} catch (e) {
1092+
throw new Error('Invalid schema object: ' + e.message);
1093+
}
10701094
}
1071-
// Use Function constructor to safely evaluate the object literal
1072-
try {
1073-
return new Function('return ' + match[1])();
1074-
} catch (e) {
1075-
throw new Error('Invalid schema object: ' + e.message);
1095+
// Try string shorthand: schema('string') or schema("string")
1096+
const stringMatch = code.match(/schema\s*\(\s*(['"])(\w+)\1\s*\)/);
1097+
if (stringMatch) {
1098+
return stringMatch[2]; // Return the type string directly
10761099
}
1100+
throw new Error('Could not find schema({...}) or schema(\'type\') in code');
10771101
}
10781102

10791103
async function init() {

0 commit comments

Comments
 (0)