Skip to content

Commit

Permalink
Merge pull request #215 from dwightjack/feat/null-type
Browse files Browse the repository at this point in the history
Add isNull type
  • Loading branch information
dwightjack authored Mar 2, 2022
2 parents 07186e4 + c84dd73 commit b1a2d5b
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 2 deletions.
7 changes: 7 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ describe('VueTypes', () => {
})
})

describe('`.nullable`', () => {
it('should proxy the `nullable` validator', () => {
const expected = getExpectDescriptors(native.nullable())
expect(getDescriptors(VueTypes.nullable)).toEqual(expected)
})
})

describe('`.custom`', () => {
it('should proxy the `custom` validator', () => {
const fn = () => true
Expand Down
6 changes: 6 additions & 0 deletions __tests__/shim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ describe('SHIM: VueTypes', () => {
})
})

describe('`.nullable`', () => {
it('should proxy the `nullable` validator', () => {
expect(VueTypes.nullable).toEqual({ type: null })
})
})

describe('SHIM: `.custom`', () => {
it('should exist', () => {
expect(VueTypes.custom).toBeInstanceOf(Function)
Expand Down
10 changes: 10 additions & 0 deletions __tests__/validators/native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,14 @@ describe('Native validators', () => {
}
})
})

describe('nullable', () => {
it('should return a validator for null', () => {
expect(native.nullable().type).toBe(null)
})
it('should not have any flag', () => {
expect((native.nullable() as any).isRequired).toBe(undefined)
expect((native.nullable() as any).def).toBe(undefined)
})
})
})
1 change: 1 addition & 0 deletions docs/guide/namespaced.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The main difference between namespaced native validators and those directly impo
| integer | `0` | - |
| symbol | - | - |
| object | `{}` | yes |
| nullable | - | - |

</div>

Expand Down
22 changes: 22 additions & 0 deletions docs/guide/validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,28 @@ props: {
}
```

### nullable

Validates that a prop is null.

```js
props: {
uniq: nullable()
}
```

::: warning
This validator **does not come with any flag or method**. It can be used with [`oneOfType`](#oneoftype) to make a **non required** prop nullable.

```js
props: {
stringOrNull: oneOfType([string(), nullable()])
}
```

**Use this validator sparingly.** Nullable props are not encouraged in Vue components, so please consider reviewing your strategy.
:::

## Custom Validators

Custom validators are a special kind of factory function useful to describe complex validation requirements. By design custom validators:
Expand Down
3 changes: 3 additions & 0 deletions examples/shared/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
shape,
toType,
fromType,
nullable,
} from 'vue-types'

/**
Expand Down Expand Up @@ -122,6 +123,8 @@ export const castedStringOrCastedObject = oneOfType([
object<UserOneOf>(),
]).def('one')

export const stringOrNull = oneOfType([string(), nullable()]).def('one')

/**
* `arrayOf` validator examples
*/
Expand Down
2 changes: 1 addition & 1 deletion examples/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"vue-types/*": ["../src/*"]
}
},
"include": ["**/*.ts", "../src/**/*.ts"]
"include": ["**/*.ts", "**/*.vue", "../src/**/*.ts"]
}
2 changes: 2 additions & 0 deletions examples/vue2/test-components/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
anyType,
objectOfTuple,
oneOfTuple,
stringOrNull,
} from '../../shared/validators'

const UserComponent = Vue.extend({
Expand All @@ -20,6 +21,7 @@ const UserComponent = Vue.extend({
hobbies: arrayOfStringsType,
randomData: arrayOfMultipleType,
score: scoreType,
maybeStr: stringOrNull,
},
})

Expand Down
2 changes: 2 additions & 0 deletions examples/vue3/test-components/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
anyType,
objectOfTuple,
oneOfTuple,
stringOrNull,
} from '../../shared/validators'

const UserComponent = defineComponent({
Expand All @@ -21,6 +22,7 @@ const UserComponent = defineComponent({
hobbies: arrayOfStringsType,
randomData: arrayOfMultipleType,
score: scoreType,
maybeStr: stringOrNull,
},
})

Expand Down
5 changes: 4 additions & 1 deletion examples/vue3/test-components/template.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<div>
<ul>
<li>User: {{ user.ID }}: {{ user.name }} - {{ age }}</li>
<li>Hobbies: {{ hobbies.join(', ') }}</li>
<li>Hobbies: {{ hobbies?.join(', ') }}</li>
<li>{{ maybeStr ?? 'hello' }}</li>
</ul>
<button @click="onClick">click</button>
</div>
Expand All @@ -20,13 +21,15 @@ import {
anyType,
objectOfTuple,
oneOfTuple,
stringOrNull,
} from '../../shared/validators'
export default defineComponent({
props: {
user: userType,
message: messageType,
age: ageType,
maybeStr: stringOrNull,
hobbies: arrayOfStringsType,
randomData: arrayOfMultipleType,
score: scoreType,
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
integer,
symbol,
object,
nullable,
} from './validators/native'
import custom from './validators/custom'
import oneOf from './validators/oneof'
Expand Down Expand Up @@ -75,6 +76,10 @@ const BaseVueTypes = /*#__PURE__*/ (() =>
return symbol()
}

static get nullable() {
return nullable()
}

static readonly custom = custom
static readonly oneOf = oneOf
static readonly instanceOf = instanceOf
Expand Down Expand Up @@ -211,6 +216,7 @@ export {
instanceOf,
objectOf,
shape,
nullable,
createTypes,
toType,
toValidableType,
Expand Down
6 changes: 6 additions & 0 deletions src/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export const shape: TypeShim = (a: any) =>
return this
},
})
export const nullable: TypeShim = () => ({
type: null,
})
/* eslint-enable @typescript-eslint/no-unused-vars */

function createValidator(
Expand Down Expand Up @@ -153,6 +156,9 @@ const BaseVueTypes = /*#__PURE__*/ (() =>
static get integer() {
return integer().def(this.defaults.integer)
}
static get nullable() {
return nullable()
}
static oneOf = oneOf
static custom = custom
static instanceOf = instanceOf
Expand Down
4 changes: 4 additions & 0 deletions src/validators/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ export const symbol = () =>
return typeof value === 'symbol'
},
})

export const nullable = () => ({
type: null as unknown as PropType<null>,
})

0 comments on commit b1a2d5b

Please sign in to comment.