Skip to content

Commit

Permalink
feat: add context to input rule
Browse files Browse the repository at this point in the history
  • Loading branch information
maticzav committed Jan 13, 2021
1 parent 259c0b1 commit ec73299
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
17 changes: 17 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: 2
updates:
# Enable version updates for npm
- package-ecosystem: 'npm'
# Look for `package.json` and `lock` files in the `root` directory
directory: '/'
# Check the npm registry for updates every day (weekdays)
schedule:
interval: 'daily'
#
- package-ecosystem: 'npm'
# Look for `package.json` and `lock` files in the `root` directory
directory: '/examples'
# Check the npm registry for updates every day (weekdays)
schedule:
interval: 'daily'
rebase-strategy: 'auto'
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ interface IRuleOptions {
}

/* Input */
function inputRule(name?: string): (yup: Yup => Yup.Schema) => Rule
function inputRule(name?: string): (yup: Yup => Yup.Schema, context: any) => Rule

/* Logic */
function and(...rules: IRule[]): LogicRule
Expand Down Expand Up @@ -436,7 +436,7 @@ const permissions = shield({
> Validate arguments using [Yup](https://github.com/jquense/yup).

```ts
function inputRule(name?: string)(yup: Yup => Yup.Schema, options?: Yup.ValidationOptions): Rule
function inputRule(name?: string)((yup: Yup, ctx: any) => Yup.Schema, options?: Yup.ValidationOptions): Rule
```

Input rule works exactly as any other rule would work. Instead of providing a complex validation rule you can simply provide a Yup validation schema which will be mached against provided arguments.
Expand Down
13 changes: 9 additions & 4 deletions src/constructors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as Yup from 'yup'
import { IRuleFunction, IRuleConstructorOptions, ShieldRule } from './types'
import {
IRuleFunction,
IRuleConstructorOptions,
ShieldRule,
IShieldContext,
} from './types'
import {
Rule,
RuleAnd,
Expand Down Expand Up @@ -67,13 +72,13 @@ export const rule = (
* @param schema
*/
export const inputRule = <T>(name?: string) => (
schema: (yup: typeof Yup) => Yup.Schema<T>,
schema: (yup: typeof Yup, ctx: IShieldContext) => Yup.Schema<T>,
options?: Yup.ValidateOptions,
) => {
if (typeof name === 'string') {
return new InputRule(name, schema(Yup), options)
return new InputRule(name, schema, options)
} else {
return new InputRule(Math.random().toString(), schema(Yup), options)
return new InputRule(Math.random().toString(), schema, options)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,18 @@ export class Rule implements IRule {
}
}

export class InputRule<Schema> extends Rule {
export class InputRule<T> extends Rule {
constructor(
name: string,
schema: Yup.Schema<Schema>,
schema: (yup: typeof Yup, ctx: IShieldContext) => Yup.Schema<T>,
options?: Yup.ValidateOptions,
) {
const validationFunction: IRuleFunction = (
parent: object,
args: object,
ctx: IShieldContext,
) =>
schema
schema(Yup, ctx)
.validate(args, options)
.then(() => true)
.catch((err) => err)
Expand Down
6 changes: 3 additions & 3 deletions tests/constructors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('input rules constructor', () => {
return schema
})
expect(JSON.stringify(rule)).toEqual(
JSON.stringify(new InputRule(name, schema)),
JSON.stringify(new InputRule(name, () => schema)),
)
})

Expand All @@ -105,7 +105,7 @@ describe('input rules constructor', () => {
return schema
})
expect(JSON.stringify(rule)).toEqual(
JSON.stringify(new InputRule(n.toString(), schema)),
JSON.stringify(new InputRule(n.toString(), () => schema)),
)
})

Expand All @@ -121,7 +121,7 @@ describe('input rules constructor', () => {
return schema
}, options)
expect(JSON.stringify(rule)).toEqual(
JSON.stringify(new InputRule(n.toString(), schema, options)),
JSON.stringify(new InputRule(n.toString(), () => schema, options)),
)
})
})
Expand Down

0 comments on commit ec73299

Please sign in to comment.