-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |