Skip to content

Commit ec74ef0

Browse files
authored
fixes typings for typescript usage (both CommonJS and ES6 modules) (#63)
* fixes typings for typescript usage (both CommonJS and ES6 modules) * updated module resolution and test reference * removed package-lock.json * added default export to source file * added missing test js file * reverted some manual testing code * reverted formatter changes to index.ts
1 parent a334e36 commit ec74ef0

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

src/FluentSchema.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ const S = (
182182
},
183183
})
184184

185-
module.exports = {
185+
const fluentSchema = {
186186
...BaseSchema(),
187187
FORMATS,
188188
TYPES,
@@ -197,3 +197,5 @@ module.exports = {
197197
null: () => S().null(),
198198
raw: fragment => S().raw(fragment),
199199
}
200+
module.exports = fluentSchema
201+
module.exports.default = fluentSchema

src/types/index.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// This file will be passed to the TypeScript CLI to verify our typings compile
2+
const S = require('../FluentSchema').default
3+
4+
console.log('isFluentSchema:', S.object().isFluentSchema)
5+
const schema = S.object()
6+
.id('http://foo.com/user')
7+
.title('A User')
8+
.description('A User desc')
9+
.definition(
10+
'address',
11+
S.object()
12+
.id('#address')
13+
.prop('line1', S.anyOf([S.string(), S.null()])) // JSON Schema nullable
14+
.prop('line2', S.string().raw({ nullable: true })) // Open API / Swagger nullable
15+
.prop('country')
16+
.allOf([S.string()])
17+
.prop('city')
18+
.prop('zipcode')
19+
)
20+
.prop('username', S.string().pattern(/[a-z]*/g))
21+
.prop('email', S.string().format('email'))
22+
.prop(
23+
'avatar',
24+
S.string()
25+
.contentEncoding('base64')
26+
.contentMediaType('image/png')
27+
)
28+
.required()
29+
.prop(
30+
'password',
31+
S.string()
32+
.default('123456')
33+
.minLength(6)
34+
.maxLength(12)
35+
.pattern('.*')
36+
)
37+
.required()
38+
.prop('addresses', S.array().items([S.ref('#address')]))
39+
.required()
40+
.prop(
41+
'role',
42+
S.object()
43+
.id('http://foo.com/role')
44+
.prop('name')
45+
.enum(['ADMIN', 'USER'])
46+
.prop('permissions')
47+
)
48+
.required()
49+
.prop('age', S.mixed(['string', 'integer']))
50+
.ifThen(S.object().prop('age', S.string()), S.required(['age']))
51+
.readOnly()
52+
.writeOnly(true)
53+
.valueOf()
54+
55+
console.log('example:\n', JSON.stringify(schema))
56+
console.log('isFluentSchema:', S.object().isFluentSchema)
57+
58+
const userBaseSchema = S.object()
59+
.additionalProperties(false)
60+
.prop('username', S.string())
61+
.prop('password', S.string())
62+
63+
const userSchema = S.object()
64+
.prop('id', S.string().format('uuid'))
65+
.prop('createdAt', S.string().format('time'))
66+
.prop('updatedAt', S.string().format('time'))
67+
.extend(userBaseSchema)
68+
.valueOf()
69+
70+
console.log('user:\n', JSON.stringify(userSchema))

src/types/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ const schema = S.object()
4747
.prop('permissions')
4848
)
4949
.required()
50-
.prop('age', S.mixed<NumberSchema & StringSchema>(['string', 'integer']))
50+
.prop(
51+
'age',
52+
S.mixed<NumberSchema & StringSchema>(['string', 'integer'])
53+
)
5154
.ifThen(S.object().prop('age', S.string()), S.required(['age']))
5255
.readOnly()
5356
.writeOnly(true)

src/types/tsconfig.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"compilerOptions": {
33
"target": "es6",
4-
"module": "commonjs",
4+
"module": "CommonJS",
55
"esModuleInterop": true,
6+
"checkJs": true,
7+
"allowJs": true,
68
"noEmit": true,
79
"strict": true
810
},
9-
"files": ["./index.ts"]
11+
"files": ["./index.ts", "index.js"]
1012
}

0 commit comments

Comments
 (0)