Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "c8 --100 node --test",
"test:typescript": "tsd",
"test:typescript": "tstyche",
"doc": "jsdoc2md ./src/*.js > docs/API.md"
},
"devDependencies": {
Expand All @@ -53,7 +53,7 @@
"jsdoc-to-markdown": "^9.0.0",
"lodash.merge": "^4.6.2",
"neostandard": "^0.13.0",
"tsd": "^0.33.0"
"tstyche": "^7.1.0"
},
"dependencies": {
"@fastify/deepmerge": "^3.0.0"
Expand Down
101 changes: 71 additions & 30 deletions types/FluentJSONSchema.test-d.ts → types/FluentJSONSchema.tst.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// This file will be passed to the TypeScript CLI to verify our typings compile
import { expect } from 'tstyche'

import S, { FluentSchemaError } from '..'

console.log('isFluentSchema:', S.object().isFluentJSONSchema)
// isFluentJSONSchema and isFluentSchema properties
expect(S.object().isFluentJSONSchema).type.toBe<boolean>()
expect(S.object().isFluentSchema).type.toBe<boolean>()

const schema = S.object()
.id('http://foo.com/user')
.title('A User')
Expand Down Expand Up @@ -46,23 +49,49 @@ const schema = S.object()
.ifThen(S.object().prop('age', S.string()), S.required(['age']))
.readOnly()
.writeOnly(true)
.valueOf()

console.log('example:\n', JSON.stringify(schema))
console.log('isFluentSchema:', S.object().isFluentSchema)
// Assertions for schema methods BEFORE valueOf
expect(schema).type.toHaveProperty('id')
expect(schema).type.toHaveProperty('title')
expect(schema).type.toHaveProperty('description')
expect(schema).type.toHaveProperty('definition')
expect(schema).type.toHaveProperty('prop')
expect(schema).type.toHaveProperty('required')
expect(schema).type.toHaveProperty('isFluentSchema')
expect(schema).type.toHaveProperty('readOnly')
expect(schema).type.toHaveProperty('writeOnly')

// String method chain assertions
expect(S.string().pattern(/[a-z]*/g)).type.toHaveProperty('pattern')
expect(S.string().format('email')).type.toHaveProperty('format')
expect(S.string().format(S.FORMATS.EMAIL)).type.toHaveProperty('format')
expect(S.string().contentEncoding('base64')).type.toHaveProperty('contentEncoding')
expect(S.string().contentMediaType('image/png')).type.toHaveProperty('contentMediaType')
expect(S.string().minLength(6)).type.toHaveProperty('minLength')
expect(S.string().maxLength(12)).type.toHaveProperty('maxLength')
expect(S.string().default('123456')).type.toHaveProperty('default')

expect(schema.valueOf()).type.toBe<Object>()
expect(S.object().isFluentSchema).type.toBe<boolean>()

const userBaseSchema = S.object()
.additionalProperties(false)
.prop('username', S.string())
.prop('password', S.string())

// ObjectSchema methods assertions
expect(userBaseSchema).type.toHaveProperty('additionalProperties')
expect(userBaseSchema).type.toHaveProperty('prop')

const userSchema = S.object()
.prop('id', S.string().format('uuid'))
.prop('createdAt', S.string().format('time'))
.prop('updatedAt', S.string().format('time'))
.extend(userBaseSchema)

console.log('user:\n', JSON.stringify(userSchema.valueOf()))
expect(userSchema).type.toHaveProperty('extend')

expect(userSchema.valueOf()).type.toBe<Object>()

const largeUserSchema = S.object()
.prop('id', S.string().format('uuid'))
Expand All @@ -73,7 +102,8 @@ const largeUserSchema = S.object()

const userSubsetSchema = largeUserSchema.only(['username', 'password'])

console.log('user subset:', JSON.stringify(userSubsetSchema.valueOf()))
expect(userSubsetSchema.valueOf()).type.toBe<Object>()
expect(userSubsetSchema).type.toHaveProperty('only')

const personSchema = S.object()
.prop('name', S.string())
Expand All @@ -84,7 +114,8 @@ const personSchema = S.object()

const bodySchema = personSchema.without(['createdAt', 'updatedAt'])

console.log('person subset:', JSON.stringify(bodySchema.valueOf()))
expect(bodySchema.valueOf()).type.toBe<Object>()
expect(bodySchema).type.toHaveProperty('without')

const personSchemaAllowsUnix = S.object()
.prop('name', S.string())
Expand All @@ -93,57 +124,63 @@ const personSchemaAllowsUnix = S.object()
.prop('createdAt', S.mixed(['string', 'integer']).format('time'))
.prop('updatedAt', S.mixed(['string', 'integer']).minimum(0))

console.log('person schema allows unix:', JSON.stringify(personSchemaAllowsUnix.valueOf()))
expect(personSchemaAllowsUnix.valueOf()).type.toBe<Object>()
expect(personSchemaAllowsUnix).type.toHaveProperty('prop')

try {
S.object().prop('foo', 'boom!' as any)
} catch (e) {
if (e instanceof FluentSchemaError) {
console.log(e.message)
expect(e.message).type.toBe<string>()
expect(e).type.toBe<FluentSchemaError>()
}
}

const arrayExtendedSchema = S.array().items(userSchema).valueOf()
const arrayExtendedSchema = S.array().items(userSchema)

console.log('array of user\n', JSON.stringify(arrayExtendedSchema))
expect(arrayExtendedSchema.valueOf()).type.toBe<Object>()
expect(arrayExtendedSchema).type.toHaveProperty('items')

const extendExtendedSchema = S.object().extend(userSchema)

console.log('extend of user\n', JSON.stringify(extendExtendedSchema))
expect(extendExtendedSchema.valueOf()).type.toBe<Object>()
expect(extendExtendedSchema).type.toHaveProperty('extend')

const rawNullableSchema = S.object()
.raw({ nullable: true })
.required(['foo', 'hello'])
.prop('foo', S.string())
.prop('hello', S.string())

console.log('raw schema with nullable props\n', JSON.stringify(rawNullableSchema))
expect(rawNullableSchema.valueOf()).type.toBe<Object>()
expect(rawNullableSchema).type.toHaveProperty('raw')
expect(rawNullableSchema).type.toHaveProperty('required')

const dependentRequired = S.object()
.dependentRequired({
foo: ['bar'],
})
.prop('foo')
.prop('bar')
.valueOf()

console.log('dependentRequired:\n', JSON.stringify(dependentRequired))
expect(dependentRequired.valueOf()).type.toBe<Object>()
expect(dependentRequired).type.toHaveProperty('dependentRequired')

const dependentSchemas = S.object()
.dependentSchemas({
foo: S.object().prop('bar'),
})
.prop('bar', S.object().prop('bar'))
.valueOf()

console.log('dependentRequired:\n', JSON.stringify(dependentSchemas))
expect(dependentSchemas.valueOf()).type.toBe<Object>()
expect(dependentSchemas).type.toHaveProperty('dependentSchemas')

const deprecatedSchema = S.object()
.deprecated()
.prop('foo', S.string().deprecated())
.valueOf()

console.log('deprecatedSchema:\n', JSON.stringify(deprecatedSchema))
expect(deprecatedSchema.valueOf()).type.toBe<Object>()
expect(deprecatedSchema).type.toHaveProperty('deprecated')

type Foo = {
foo: string
Expand All @@ -156,25 +193,26 @@ const dependentRequiredWithType = S.object<Foo>()
})
.prop('foo')
.prop('bar')
.valueOf()

console.log('dependentRequired:\n', JSON.stringify(dependentRequiredWithType))
expect(dependentRequiredWithType.valueOf()).type.toBe<Object>()
expect(dependentRequiredWithType).type.toHaveProperty('dependentRequired')
expect(dependentRequiredWithType).type.toHaveProperty('prop')

const dependentSchemasWithType = S.object<Foo>()
.dependentSchemas({
foo: S.object().prop('bar'),
})
.prop('bar', S.object().prop('bar'))
.valueOf()

console.log('dependentSchemasWithType:\n', JSON.stringify(dependentSchemasWithType))
expect(dependentSchemasWithType.valueOf()).type.toBe<Object>()
expect(dependentSchemasWithType).type.toHaveProperty('dependentSchemas')

const deprecatedSchemaWithType = S.object<Foo>()
.deprecated()
.prop('foo', S.string().deprecated())
.valueOf()

console.log('deprecatedSchemaWithType:\n', JSON.stringify(deprecatedSchemaWithType))
expect(deprecatedSchemaWithType.valueOf()).type.toBe<Object>()
expect(deprecatedSchemaWithType).type.toHaveProperty('deprecated')

type ReallyLongType = {
foo: string
Expand All @@ -194,13 +232,16 @@ const deepTestOnTypes = S.object<ReallyLongType>()
// you can provide any string, to avoid breaking changes
.prop('aaaa', S.anyOf([S.string()]))
.definition('abcd', S.number())
.valueOf()

console.log('deepTestOnTypes:\n', JSON.stringify(deepTestOnTypes))
expect(deepTestOnTypes.valueOf()).type.toBe<Object>()
expect(deepTestOnTypes).type.toHaveProperty('prop')
expect(deepTestOnTypes).type.toHaveProperty('definition')

const tsIsoSchema = S.object()
.prop('createdAt', S.string().format('iso-time'))
.prop('updatedAt', S.string().format('iso-date-time'))
.valueOf()

console.log('ISO schema OK:', JSON.stringify(tsIsoSchema))
expect(tsIsoSchema.valueOf()).type.toBe<Object>()
expect(tsIsoSchema).type.toHaveProperty('prop')
expect(S.string().format('iso-time')).type.toHaveProperty('format')
expect(S.string().format('iso-date-time')).type.toHaveProperty('format')
Loading