Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: andnp/ValidTyped
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.0.0
Choose a base ref
...
head repository: andnp/ValidTyped
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.1.0
Choose a head ref
  • 9 commits
  • 6 files changed
  • 3 contributors

Commits on Jul 4, 2018

  1. Copy the full SHA
    d89dcac View commit details
  2. Copy the full SHA
    b89041e View commit details
  3. docs(readme): add greenkeeper badge to readme generator

    andy.patterson committed Jul 4, 2018

    Unverified

    No user is associated with the committer email.
    Copy the full SHA
    d9400ea View commit details
  4. docs: generate documentation

    andy.patterson committed Jul 4, 2018

    Unverified

    No user is associated with the committer email.
    Copy the full SHA
    d87d8d7 View commit details
  5. chore: lock typescript to 2.8

    We cannot use typescript 2.9 because the autogenerated .d.ts files use
    the new `import('thing.d.ts')` syntax, which is incompatible with
    previous TS versions. In favor of making the lib useable for older TS
    versions, we should stay at 2.8 until we _need_ to use something from a
    higher version.
    andy.patterson committed Jul 4, 2018

    Unverified

    No user is associated with the committer email.
    Copy the full SHA
    5899d18 View commit details
  6. Merge pull request #11 from andnp/greenkeeper/initial

    Update dependencies to enable Greenkeeper 🌴
    andnp authored Jul 4, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    3adf0b1 View commit details

Commits on Jul 19, 2018

  1. feat: add ability to perform additional validations

    andy.patterson committed Jul 19, 2018

    Unverified

    No user is associated with the committer email.
    Copy the full SHA
    781725f View commit details
  2. docs: generate documentation

    andy.patterson committed Jul 19, 2018

    Unverified

    No user is associated with the committer email.
    Copy the full SHA
    71d1c16 View commit details
  3. Merge pull request #14 from andnp/OtherValidations

    Other validations
    andnp authored Jul 19, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    d89c378 View commit details
Showing with 90 additions and 6 deletions.
  1. +17 −0 README.md
  2. +4 −4 package.json
  3. +1 −0 scripts/generateDocumentation.ts
  4. +47 −2 src/index.ts
  5. +11 −0 tests/unit/array.test.ts
  6. +10 −0 tests/unit/string.test.ts
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# validtyped
[![Build Status](https://travis-ci.org/andnp/ValidTyped.svg?branch=master)](https://travis-ci.org/andnp/ValidTyped)
[![Greenkeeper badge](https://badges.greenkeeper.io/andnp/ValidTyped.svg)](https://greenkeeper.io/)

A runtime and compile-time type checker library.

@@ -343,6 +344,22 @@ if (result.valid) doThing(result.data);
else logger.error(...result.errors);
```

### Validator.withOptions
Add additional validations to the generated schema.
While most of these validations are not representable at compile time
with typescript (`minLength` of a `string` for instance), it can be helpful
to have the additional validations when validating runtime types.

| Param | Description |
| --- | --- |
| opts | JSON schema specific options (for instance: `{ maxLength: 2, minLength: 0 }`) |
###### Example:
```typescript
const validator = v.string().withOptions({ minLength: 1 });
validator.isValid(''); // false
validator.isValid('hi'); // true
```

### ValidType
Returns the encapsulated type of a `Validator` type.
###### Example:
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -33,13 +33,13 @@
},
"devDependencies": {
"@commitlint/config-conventional": "^7.0.0",
"@types/jest": "~23.0.0",
"@types/jest": "~23.1.4",
"@types/node": "^10.3.5",
"commitlint": "^7.0.0",
"husky": "^0.14.3",
"jest": "^22.4.3",
"ts-jest": "^22.4.2",
"ts-node": "^6.0.0",
"jest": "^23.3.0",
"ts-jest": "^23.0.0",
"ts-node": "^7.0.0",
"tslint": "^5.9.1",
"typescript": "~2.8.1"
},
1 change: 1 addition & 0 deletions scripts/generateDocumentation.ts
Original file line number Diff line number Diff line change
@@ -144,6 +144,7 @@ const generateMarkdown = (fileName: string) => {
const headerString =
`# validtyped
[![Build Status](https://travis-ci.org/andnp/ValidTyped.svg?branch=master)](https://travis-ci.org/andnp/ValidTyped)
[![Greenkeeper badge](https://badges.greenkeeper.io/andnp/ValidTyped.svg)](https://greenkeeper.io/)
A runtime and compile-time type checker library.
49 changes: 47 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import { Schema, SchemaMetaData } from 'type-level-schema/schema';
import { objectKeys, Nominal, AnyFunc, AllRequired, Optional, Unknown, PlainObject } from 'simplytyped';
import { objectKeys, Nominal, AnyFunc, AllRequired, Optional, Unknown, PlainObject, Omit } from 'simplytyped';
import * as Ajv from 'ajv';

// Schema definitions
import { Schema, SchemaMetaData } from 'type-level-schema/schema';
import { StringSchema } from 'type-level-schema/defs/string';
import { NumberSchema } from 'type-level-schema/defs/number';
import { BooleanSchema } from 'type-level-schema/defs/boolean';
import { ArraySchema } from 'type-level-schema/defs/array';
import { ObjectSchema } from 'type-level-schema/defs/object';

/**
* no-doc - Gets the full schema definition for a given type.
*/
export type TypeToSchema<T> =
T extends string ? StringSchema :
T extends number ? NumberSchema :
T extends boolean ? BooleanSchema :
T extends any[] ? ArraySchema :
T extends object ? ObjectSchema :
never;

/**
* no-doc - Gets the optional parts of the schema definition for a given type.
*/
export type TypeToSchemaOptions<T> = Omit<TypeToSchema<T>, 'type'>;

/**
* no-doc - Generates an object type from `[string, Validator]` pairs
* @param O a Validator record
@@ -182,6 +205,28 @@ export class Validator<T> {
return this;
}

/**
* Add additional validations to the generated schema.
* While most of these validations are not representable at compile time
* with typescript (`minLength` of a `string` for instance), it can be helpful
* to have the additional validations when validating runtime types.
* @param opts JSON schema specific options (for instance: `{ maxLength: 2, minLength: 0 }`)
*
* @example
* ```typescript
* const validator = v.string().withOptions({ minLength: 1 });
* validator.isValid(''); // false
* validator.isValid('hi'); // true
* ```
*/
withOptions(opts: TypeToSchemaOptions<T>): this {
this.schema = {
...this.schema,
...opts as any,
};
return this;
}

/**
* Creates a new validator that is true whenever the data matches `this` _or_ `v`.
* @param other Another validator instance whose type will form a union with `this` encapsulated type.
11 changes: 11 additions & 0 deletions tests/unit/array.test.ts
Original file line number Diff line number Diff line change
@@ -30,3 +30,14 @@ test('Can validate a deep array', () => {
fail();
}
});

test('Can add JSON Schema options', () => {
const x: any = [ 'yellow!' ];

const validator = v.array(v.any())
.withOptions({ minItems: 2 });

// expect to fail because we have fewer than 2 items
if (validator.isValid(x)) fail();
else pass();
});
10 changes: 10 additions & 0 deletions tests/unit/string.test.ts
Original file line number Diff line number Diff line change
@@ -35,3 +35,13 @@ test('String is not in enum', () => {
if (validator.isValid(x)) fail();
else pass();
});

test('Can add JSON Schema options', () => {
const x: any = '';

const validator = v.string()
.withOptions({ minLength: 1 });

if (validator.isValid(x)) fail();
else pass();
});