Skip to content

Instantly share code, notes, and snippets.

@yuda110
Last active August 12, 2020 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuda110/4854adfa76b2eaabd6998c3d037ac6c6 to your computer and use it in GitHub Desktop.
Save yuda110/4854adfa76b2eaabd6998c3d037ac6c6 to your computer and use it in GitHub Desktop.
...
<p-field-group :label="name"
:invalid-text="nameInvalidText"
:invalid="!nameValidator.valid"
:required="true"
>
...
<script lang="ts">
import { validate } from 'jsonschema';
export default {
setup(){
const state = reactive({
name: '',
priority: 0,
nameValidator: computed(() => validate(state.name, {
type: 'string',
minLength: 2,
message: {
minLength: 'should NOT be shorter than 2 characters',
},
})),
priorityValidator: computed(() => validate(state.priority, {
type: 'number',
required: true,
minimum: 1,
maximum: 10,
message: {
minimum: 'should be >= 1',
maximum: 'should be <= 10',
},
})),
isValid: computed(() => state.nameValidator.valid && state.priorityValidator.valid),
nameInvalidText: computed(() => state.nameValidator.schema.message[state.nameValidator.errors[0]?.name]),
priorityInvalidText: computed(() => state.priorityValidator.schema.message[state.priorityValidator.errors[0]?.name]),
});
return {
...toRefs(state),
};
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment