Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Commit

Permalink
feat: add support for pre-parsed schema
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 8, 2019
1 parent 4467896 commit 8781166
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 21 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"mrm": "mrm --preset=@adonisjs/mrm-preset",
"pretest": "npm run lint",
"test": "node japaFile.js",
"lint": "eslint **/*.ts",
"lint": "eslint . --ext=.ts",
"clean": "del build",
"compile": "npm run lint && npm run clean && tsc",
"build": "npm run compile",
Expand Down Expand Up @@ -48,14 +48,14 @@
"del-cli": "^3.0.0",
"doctoc": "^1.4.0",
"eslint": "^6.7.2",
"eslint-plugin-adonis": "^1.0.1",
"eslint-plugin-adonis": "^1.0.2",
"husky": "^3.1.0",
"japa": "^3.0.1",
"mrm": "^2.0.0",
"np": "^5.2.1",
"reflect-metadata": "^0.1.13",
"ts-node": "^8.5.4",
"typescript": "^3.7.2"
"typescript": "^3.7.3"
},
"nyc": {
"exclude": [
Expand All @@ -81,9 +81,9 @@
"publish": false
},
"dependencies": {
"indicative-compiler": "^7.1.0",
"indicative-compiler": "^7.2.0",
"indicative-formatters": "^7.2.2",
"indicative-parser": "^7.0.3",
"indicative-parser": "^7.1.1",
"indicative-rules": "^7.2.2"
}
}
8 changes: 4 additions & 4 deletions src/Contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* file that was distributed with this source code.
*/

import { Schema, Messages } from 'indicative-parser'
import { ErrorFormatterContract } from 'indicative-compiler'
import { ErrorCollectorFn } from 'indicative-compiler/build/src/contracts'
import { Schema, Messages, TypedSchema, ParsedTypedSchema } from 'indicative-parser'

/**
* Shape of validator config
Expand All @@ -36,12 +36,12 @@ export type SanitizerConfig = {
/**
* Shape of `validate` and `validateAll` function.
*/
export type ValidateFn = (
export type ValidateFn = <T extends ParsedTypedSchema<TypedSchema> | Schema>(
data: any,
schema: Schema,
schema: T,
messages?: Messages,
config?: Partial<ValidatorConfig>,
) => Promise<any>
) => T extends Schema ? Promise<any> : Promise<T['props']>

/**
* Shape of `sanitize` function.
Expand Down
5 changes: 3 additions & 2 deletions src/Sanitizer/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@
*/

import { sanitizations } from 'indicative-rules'
import { ParsedRule } from 'indicative-parser'
import { SanitizationDefination } from 'indicative-compiler'
import { sanitizations as sanitizationsList } from './sanitizations'

/**
* Extend validator by adding new rules
*/
export function extend (name: string, definition: SanitizationDefination) {
export function extend (name: string, definition: SanitizationDefination): void {
sanitizations[name] = definition

/**
* Also adding it to the sanitizations list used to define
* schema rules
*/
sanitizationsList[name] = function rule (args) {
sanitizationsList[name] = function rule (args): ParsedRule {
return { name, args: args || [] }
}
}
2 changes: 1 addition & 1 deletion src/Sanitizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const cacheManager = new CacheManager<ReturnType<SanitizerCompiler['compile']>>(
/**
* Returns executor by pre-compiling and optionally caching schema.
*/
function getExecutor (schema: Schema, config: SanitizerConfig) {
function getExecutor (schema: Schema, config: SanitizerConfig): SanitizerExecutor {
/**
* Always compile schema, when there is no cacheKey
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { config as validatorConfig } from './config'
/**
* Configure global validation options
*/
export function configure (config: Partial<ValidatorConfig>) {
export function configure (config: Partial<ValidatorConfig>): void {
Object.assign(validatorConfig, config)
}

Expand Down
5 changes: 3 additions & 2 deletions src/Validator/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@
*/

import { validations } from 'indicative-rules'
import { ParsedRule } from 'indicative-parser'
import { ValidationDefination } from 'indicative-compiler'
import { validations as validationsList } from './validations'

/**
* Extend validator by adding new rules
*/
export function extend (name: string, definition: ValidationDefination) {
export function extend (name: string, definition: ValidationDefination): void {
validations[name] = definition

/**
* Also adding it to the validations list used to define
* schema rules
*/
validationsList[name] = function rule (args) {
validationsList[name] = function rule (args): ParsedRule {
return { name, args: args || [] }
}
}
8 changes: 4 additions & 4 deletions src/Validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
*/

import { validations } from 'indicative-rules'
import { Schema, Messages } from 'indicative-parser'
import { ValidatorCompiler, ValidatorExecutor } from 'indicative-compiler'
import { Schema, Messages, ParsedTypedSchema, TypedSchema } from 'indicative-parser'

import { CacheManager } from '../CacheManager'
import { config as validatorConfig } from './config'
Expand All @@ -25,7 +25,7 @@ const cacheManager = new CacheManager<ReturnType<ValidatorCompiler['compile']>>(
* Returns executor by pre-compiling and optionally caching schema.
*/
function getExecutor (
schema: Schema,
schema: Schema | ParsedTypedSchema<TypedSchema>,
messages: Messages,
config: ValidatorConfig,
): ValidatorExecutor {
Expand Down Expand Up @@ -68,7 +68,7 @@ export const validate: ValidateFn = (data, schema, messages, config?) => {
true,
config.removeAdditional!,
config.customErrorCollector,
)
) as any
}

/**
Expand All @@ -89,5 +89,5 @@ export const validateAll: ValidateFn = (data, schema, messages, config?) => {
false,
config.removeAdditional!,
config.customErrorCollector,
)
) as any
}
33 changes: 33 additions & 0 deletions test/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import test from 'japa'
import { t } from 'indicative-parser'
import { validate, validateAll } from '../src/Validator'

test.group('validate', () => {
Expand Down Expand Up @@ -47,6 +48,22 @@ test.group('validate', () => {
}])
}
})

test('run validations on pre-parsed schema', async (assert) => {
assert.plan(1)

try {
await validate({}, t.schema({
username: t.string(),
}))
} catch (errors) {
assert.deepEqual(errors, [{
message: 'required validation failed on username',
validation: 'required',
field: 'username',
}])
}
})
})

test.group('validateAll', () => {
Expand Down Expand Up @@ -100,4 +117,20 @@ test.group('validateAll', () => {
])
}
})

test('run validations on pre-parsed schema', async (assert) => {
assert.plan(1)

try {
await validateAll({}, t.schema({
username: t.string(),
}))
} catch (errors) {
assert.deepEqual(errors, [{
message: 'required validation failed on username',
validation: 'required',
field: 'username',
}])
}
})
})
5 changes: 3 additions & 2 deletions validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
* file that was distributed with this source code.
*/

export { validate, validateAll } from './src/Validator'
export { configure } from './src/Validator/configure'
export { t } from 'indicative-parser'
export { extend } from './src/Validator/extend'
export { configure } from './src/Validator/configure'
export { validate, validateAll } from './src/Validator'
export { validations } from './src/Validator/validations'
export { ValidationRulesContract } from 'indicative-rules'

0 comments on commit 8781166

Please sign in to comment.