Skip to content

Commit

Permalink
Add Typescript types (issue #13)
Browse files Browse the repository at this point in the history
  • Loading branch information
aboutlo committed Dec 20, 2018
1 parent 73fee76 commit 08c04e4
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 3 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.3.1",
"description": "JSON Schema fluent API",
"main": "src/FluentSchema.js",
"typings": "src/FluentSchema.d.ts",
"keywords": [
"JSON",
"schema",
Expand Down Expand Up @@ -35,7 +36,7 @@
},
"lint-staged": {
"linters": {
"*.{json,md,js}": [
"*.{json,md,js,ts}": [
"prettier --write",
"git add"
]
Expand All @@ -45,8 +46,9 @@
]
},
"scripts": {
"test": "jest src/**.test.js --verbose",
"test": "jest src/**.test.js --verbose && yarn typescript",
"test:watch": "jest src/**.test.js --verbose --watch",
"typescript": "tsc --project ./src/types/tsconfig.json",
"format": "prettier --write ./src/**/*.js",
"coverage": "jest src/**.test.js --coverage",
"doc": "jsdoc2md ./src/FluentSchema.js > docs/API.md"
Expand All @@ -57,7 +59,8 @@
"jest": "^23.6.0",
"jsdoc-to-markdown": "^4.0.1",
"lint-staged": "^8.0.4",
"prettier": "^1.14.3"
"prettier": "^1.14.3",
"typescript": "^3.2.2"
},
"dependencies": {}
}
86 changes: 86 additions & 0 deletions src/FluentSchema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
declare namespace FluentSchema {
function FluentSchema(opt?: SchemaOptions): FluentSchema

type FORMATS =
| 'relative-json-pointer'
| 'json-pointer'
| 'uuid'
| 'regex'
| 'ipv6'
| 'ipv4'
| 'hostname'
| 'email'
| 'url'
| 'uri-template'
| 'uri-reference'
| 'uri'
| 'time'
| 'date'
| 'date-time'

interface SchemaOptions {
schema: object
generateIds: boolean
}

interface PatternPropertiesOptions {
[key: string]: FluentSchema
}

interface DependenciesOptions {
[key: string]: FluentSchema[]
}

interface FluentSchema {
id: (id: string) => FluentSchema
title: (title: string) => FluentSchema
description: (description: string) => FluentSchema
examples: (examples: Array<any>) => FluentSchema
ref: (ref: string) => FluentSchema
definition: (name: string, props?: FluentSchema) => FluentSchema
prop: (name: string, props?: FluentSchema) => FluentSchema
enum: (values: Array<any>) => FluentSchema
const: (value: any) => FluentSchema
default: (value: any) => FluentSchema
required: () => FluentSchema
not: () => FluentSchema
anyOf: (attributes: Array<FluentSchema>) => FluentSchema
allOf: (attributes: Array<FluentSchema>) => FluentSchema
oneOf: (attributes: Array<FluentSchema>) => FluentSchema
asString: () => FluentSchema
minLength: (min: number) => FluentSchema
maxLength: (min: number) => FluentSchema
format: (format: FORMATS) => FluentSchema
pattern: (pattern: string) => FluentSchema
asNumber: () => FluentSchema
minimum: (min: number) => FluentSchema
exclusiveMinimum: (min: number) => FluentSchema
maximum: (max: number) => FluentSchema
exclusiveMaximum: (max: number) => FluentSchema
multipleOf: (multiple: number) => FluentSchema
asBoolean: () => FluentSchema
asInteger: () => FluentSchema
asArray: () => FluentSchema
items: (items: FluentSchema | FluentSchema[]) => FluentSchema
additionalItems: (items: FluentSchema | boolean) => FluentSchema
contains: (value: FluentSchema | boolean) => FluentSchema
uniqueItems: (boolean: boolean) => FluentSchema
minItems: (min: number) => FluentSchema
maxItems: (min: number) => FluentSchema
asObject: (min: number) => FluentSchema
additionalProperties: (value: FluentSchema | boolean) => FluentSchema
maxProperties: (max: number) => FluentSchema
minProperties: (min: number) => FluentSchema
patternProperties: (options: PatternPropertiesOptions) => FluentSchema
dependencies: (options: DependenciesOptions) => FluentSchema
propertyNames: (value: FluentSchema) => FluentSchema
asNull: () => FluentSchema
ifThen: (ifClause: FluentSchema, thenClause: FluentSchema) => FluentSchema
ifThenElse: (
ifClause: FluentSchema,
thenClause: FluentSchema,
elseClause: FluentSchema
) => FluentSchema
}
}
export = FluentSchema
44 changes: 44 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// This file will be passed to the TypeScript CLI to verify our typings compile

import { FluentSchema } from '../FluentSchema'

const schema = FluentSchema()
.id('http://foo.com/user')
.title('A User')
.description('A User desc')
.definition(
'address',
FluentSchema()
.id('#address')
.prop('country')
.allOf([FluentSchema().asString()])
.prop('city')
.prop('zipcode')
)
.prop('username')
.asString()
.required()
.prop('password')
.asString()
.default('123456')
.minLength(6)
.maxLength(12)
.pattern('.*')
.required()
.prop('address')
.ref('#address')
.required()
.prop(
'role',
FluentSchema()
.id('http://foo.com/role')
.prop('name')
.enum(['ADMIN', 'USER'])
.prop('permissions')
)
.required()
.prop('age')
.asInteger()
.valueOf()

console.log({ schema })
9 changes: 9 additions & 0 deletions src/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"noEmit": true,
"strict": true
},
"files": ["./index.ts"]
}

0 comments on commit 08c04e4

Please sign in to comment.