Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Schemastery Gen
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
3rdeye
Schemastery Gen
Commits
9f2bf284
Commit
9f2bf284
authored
Jan 31, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
buggy
parent
950956b7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
173 additions
and
13 deletions
+173
-13
src/decorators/define-property.ts
src/decorators/define-property.ts
+16
-1
src/decorators/register.ts
src/decorators/register.ts
+69
-8
src/def/interfaces.ts
src/def/interfaces.ts
+17
-4
tests/recurse-schema.spec.ts
tests/recurse-schema.spec.ts
+71
-0
No files found.
src/decorators/define-property.ts
View file @
9f2bf284
import
{
ClassType
,
SchemaOptions
}
from
'
../def/interfaces
'
;
import
{
ClassType
,
RefSym
,
SchemaOptions
,
SchemaReference
,
SchemaType
,
}
from
'
../def/interfaces
'
;
import
{
Metadata
}
from
'
../metadata/metadata
'
;
import
{
Metadata
}
from
'
../metadata/metadata
'
;
import
{
import
{
SetTransformer
,
SetTransformer
,
...
@@ -55,3 +61,12 @@ export function SchemaProperty(options: SchemaOptions = {}): PropertyDecorator {
...
@@ -55,3 +61,12 @@ export function SchemaProperty(options: SchemaOptions = {}): PropertyDecorator {
// for backward compatibility
// for backward compatibility
export
const
DefineSchema
=
SchemaProperty
;
export
const
DefineSchema
=
SchemaProperty
;
export
function
SchemaRef
<
S
=
any
,
T
=
S
>
(
factory
:
()
=>
SchemaType
<
S
,
T
>
,
):
SchemaReference
<
S
,
T
>
{
return
{
[
RefSym
]:
true
,
factory
,
};
}
src/decorators/register.ts
View file @
9f2bf284
import
{
import
{
ClassType
,
ClassType
,
RefSym
,
SchemaClassOptions
,
SchemaClassOptions
,
SchemaOptions
,
SchemaOptions
,
SchemaOptionsDict
,
SchemaOptionsDict
,
SchemaOrReference
,
SchemaReference
,
SchemaType
,
}
from
'
../def
'
;
}
from
'
../def
'
;
import
Schema
from
'
schemastery
'
;
import
Schema
from
'
schemastery
'
;
import
{
reflector
}
from
'
../metadata/reflector
'
;
import
{
reflector
}
from
'
../metadata/reflector
'
;
...
@@ -10,11 +14,11 @@ import _ from 'lodash';
...
@@ -10,11 +14,11 @@ import _ from 'lodash';
import
{
Metadata
}
from
'
../metadata/metadata
'
;
import
{
Metadata
}
from
'
../metadata/metadata
'
;
import
{
kSchema
}
from
'
../utility/kschema
'
;
import
{
kSchema
}
from
'
../utility/kschema
'
;
function
getBasePropertySchemaFromOptions
(
options
:
SchemaOptions
)
{
function
resolveSchemaType
(
schemaType
:
SchemaType
):
SchemaOrReference
{
if
(
options
.
schema
)
{
if
(
schemaType
&&
schemaType
[
RefSym
]
)
{
return
options
.
schema
;
return
schemaType
as
SchemaReference
;
}
}
switch
(
options
.
t
ype
as
string
)
{
switch
(
schemaT
ype
as
string
)
{
case
'
any
'
:
case
'
any
'
:
return
Schema
.
any
();
return
Schema
.
any
();
case
'
never
'
:
case
'
never
'
:
...
@@ -28,7 +32,7 @@ function getBasePropertySchemaFromOptions(options: SchemaOptions) {
...
@@ -28,7 +32,7 @@ function getBasePropertySchemaFromOptions(options: SchemaOptions) {
case
'
object
'
:
case
'
object
'
:
return
Schema
.
object
({}).
default
({});
return
Schema
.
object
({}).
default
({});
default
:
default
:
return
Schema
.
from
(
options
.
t
ype
);
return
Schema
.
from
(
schemaT
ype
);
}
}
}
}
...
@@ -36,8 +40,14 @@ function applyOptionsToSchema(schema: Schema, options: SchemaClassOptions) {
...
@@ -36,8 +40,14 @@ function applyOptionsToSchema(schema: Schema, options: SchemaClassOptions) {
Object
.
assign
(
schema
.
meta
,
options
);
Object
.
assign
(
schema
.
meta
,
options
);
}
}
function
getPropertySchemaFromOptions
<
PT
>
(
options
:
SchemaOptions
):
Schema
<
PT
>
{
function
getPropertySchemaFromOptions
<
PT
>
(
let
schema
=
getBasePropertySchemaFromOptions
(
options
);
options
:
SchemaOptions
,
):
SchemaOrReference
<
PT
>
{
const
_schema
=
resolveSchemaType
(
options
.
type
);
if
(
_schema
[
RefSym
])
{
return
_schema
as
SchemaReference
<
PT
>
;
}
let
schema
=
_schema
as
Schema
;
if
(
options
.
dict
)
{
if
(
options
.
dict
)
{
const
skeySchema
=
(
const
skeySchema
=
(
options
.
dict
===
true
options
.
dict
===
true
...
@@ -53,10 +63,61 @@ function getPropertySchemaFromOptions<PT>(options: SchemaOptions): Schema<PT> {
...
@@ -53,10 +63,61 @@ function getPropertySchemaFromOptions<PT>(options: SchemaOptions): Schema<PT> {
return
schema
;
return
schema
;
}
}
function
resolveSchemaReference
<
S
=
any
,
T
=
S
>
(
ref
:
SchemaReference
<
S
,
T
>
)
{
const
value
=
ref
.
factory
();
if
(
typeof
value
===
'
function
'
&&
!
value
[
kSchema
])
{
return
SchemaClass
(
value
as
{
new
(...
args
:
any
[]):
any
});
}
return
value
;
}
function
resolvePropertySchemaFromOptions
<
PT
>
(
options
:
SchemaOptions
,
):
Schema
<
PT
>
{
const
schema
=
getPropertySchemaFromOptions
<
PT
>
(
options
);
if
(
schema
[
RefSym
])
{
return
resolvePropertySchemaFromOptions
({
...
options
,
type
:
(
schema
as
SchemaReference
<
PT
>
).
factory
(),
});
}
return
schema
as
Schema
<
PT
>
;
}
function
tempDefGet
<
T
,
K
extends
keyof
T
>
(
obj
:
T
,
key
:
K
,
getter
:
()
=>
T
[
K
])
{
Object
.
defineProperty
(
obj
,
key
,
{
get
:
getter
,
set
:
(
value
:
T
[
K
])
=>
{
Object
.
defineProperty
(
obj
,
key
,
{
enumerable
:
true
,
configurable
:
true
,
writable
:
true
,
value
,
});
},
enumerable
:
true
,
configurable
:
true
,
});
}
function
schemasFromDict
<
T
>
(
dict
:
SchemaOptionsDict
<
T
>
)
{
function
schemasFromDict
<
T
>
(
dict
:
SchemaOptionsDict
<
T
>
)
{
const
schemaDict
:
{
const
schemaDict
:
{
[
P
in
keyof
SchemaOptionsDict
<
T
>
]:
Schema
<
SchemaOptionsDict
<
T
>
[
P
]
>
;
[
P
in
keyof
SchemaOptionsDict
<
T
>
]:
SchemaOrReference
<
SchemaOptionsDict
<
T
>
[
P
]
>
;
}
=
_
.
mapValues
(
dict
,
(
opt
)
=>
getPropertySchemaFromOptions
(
opt
));
}
=
_
.
mapValues
(
dict
,
(
opt
)
=>
getPropertySchemaFromOptions
(
opt
));
for
(
const
_key
of
Object
.
keys
(
schemaDict
))
{
const
key
=
_key
as
keyof
T
;
if
(
schemaDict
[
key
][
RefSym
])
{
const
schemaOptions
=
dict
[
key
];
tempDefGet
(
schemaDict
,
key
,
()
=>
resolvePropertySchemaFromOptions
({
...
schemaOptions
,
type
:
resolveSchemaReference
(
schemaDict
[
key
]
as
SchemaReference
),
}),
);
}
}
return
Schema
.
object
(
schemaDict
);
return
Schema
.
object
(
schemaDict
);
}
}
...
...
src/def/interfaces.ts
View file @
9f2bf284
...
@@ -10,23 +10,36 @@ export type SchemaNativeType =
...
@@ -10,23 +10,36 @@ export type SchemaNativeType =
|
'
any
'
|
'
any
'
|
'
never
'
;
|
'
never
'
;
export
type
SchemaSource
=
export
type
SchemaSource
<
S
=
any
,
T
=
S
>
=
|
null
|
null
|
undefined
|
undefined
|
string
|
string
|
number
|
number
|
boolean
|
boolean
|
Schema
|
Schema
<
S
,
T
>
// eslint-disable-next-line @typescript-eslint/ban-types
// eslint-disable-next-line @typescript-eslint/ban-types
|
Function
|
Function
|
{
new
(...
args
:
any
[]):
any
};
|
{
new
(...
args
:
any
[]):
any
};
export
type
SchemaType
=
SchemaNativeType
|
SchemaSource
;
export
const
RefSym
=
Symbol
(
'
SchemasteryGenRef
'
);
export
interface
SchemaReference
<
S
=
any
,
T
=
S
>
{
[
RefSym
]:
true
;
factory
:
()
=>
SchemaType
<
S
,
T
>
;
}
export
type
SchemaOrReference
<
S
=
any
,
T
=
S
>
=
|
Schema
<
S
,
T
>
|
SchemaReference
<
S
,
T
>
;
export
type
SchemaType
<
S
=
any
,
T
=
S
>
=
|
SchemaNativeType
|
SchemaSource
<
S
,
T
>
|
SchemaReference
<
S
,
T
>
;
export
type
SchemaClassOptions
=
Schema
.
Meta
<
any
>
;
export
type
SchemaClassOptions
=
Schema
.
Meta
<
any
>
;
export
interface
SchemaOptions
extends
SchemaClassOptions
{
export
interface
SchemaOptions
extends
SchemaClassOptions
{
schema
?:
Schema
<
any
>
;
dict
?:
boolean
|
Schema
<
any
,
string
>
|
string
;
dict
?:
boolean
|
Schema
<
any
,
string
>
|
string
;
array
?:
boolean
;
array
?:
boolean
;
type
?:
SchemaType
;
type
?:
SchemaType
;
...
...
tests/recurse-schema.spec.ts
0 → 100644
View file @
9f2bf284
import
{
DefineSchema
,
RegisterSchema
,
SchemaRef
}
from
'
../src/decorators
'
;
import
{
kSchema
}
from
'
../src/utility/kschema
'
;
import
Schema
from
'
schemastery
'
;
@
RegisterSchema
()
class
Post
{
constructor
(
_
:
Partial
<
Post
>
)
{}
@
DefineSchema
()
name
:
string
;
getName
?()
{
return
this
.
name
;
}
@
DefineSchema
({
type
:
SchemaRef
(()
=>
Post
),
})
child
?:
Post
;
}
describe
(
'
Recurse schema
'
,
()
=>
{
it
(
'
should be a Schema
'
,
()
=>
{
expect
(
Post
[
kSchema
]).
toBeDefined
();
});
it
(
'
child should be Schema
'
,
()
=>
{
const
childPost
=
(
Post
as
Schema
<
Post
>
).
dict
.
child
;
expect
(
childPost
[
kSchema
]).
toBeDefined
();
expect
(
childPost
===
Post
);
});
it
(
'
child of child should be Schema
'
,
()
=>
{
const
childPost
=
(
Post
as
Schema
<
Post
>
).
dict
.
child
.
dict
.
child
;
expect
(
childPost
[
kSchema
]).
toBeDefined
();
expect
(
childPost
===
Post
);
});
it
(
'
should resolve value
'
,
()
=>
{
const
post
=
new
Post
({
name
:
'
test
'
,
});
expect
(
post
.
getName
()).
toBe
(
'
test
'
);
});
it
(
'
should be able to recurse
'
,
()
=>
{
const
post
=
new
Post
({
name
:
'
test
'
,
child
:
{
name
:
'
test
'
,
},
});
expect
(
post
.
child
.
getName
()).
toBe
(
'
test
'
);
});
it
(
'
should recurse twice
'
,
()
=>
{
const
post
=
new
Post
({
name
:
'
test
'
,
child
:
{
name
:
'
test
'
,
child
:
{
name
:
'
test
'
,
},
},
});
expect
(
post
.
getName
()).
toBe
(
'
test
'
);
expect
(
post
.
child
.
getName
()).
toBe
(
'
test
'
);
expect
(
post
.
child
.
child
.
getName
()).
toBe
(
'
test
'
);
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment