Skip to content

Commit 3dac65a

Browse files
authored
Merge pull request #205 from citation-file-format/166-validate-file
add file validation
2 parents f3a0eaa + 384a07d commit 3dac65a

4 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/components/Preview.vue

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,24 @@
3535
<pre>{{ cffstr }}</pre>
3636
</q-card-section>
3737
</q-card>
38+
<div class="row justify-center q-pt-md">
39+
<p>
40+
Your CITATION.cff is&nbsp;
41+
</p>
42+
<span v-if="isValid">valid</span>
43+
<span v-else>invalid</span>
44+
</div>
3845
</template>
3946

4047
<script lang="ts">
41-
import { defineComponent, ref } from 'vue'
48+
import { defineComponent, ref, computed } from 'vue'
4249
import { useCffstr } from 'src/store/cffstr'
50+
import { isValidCffFile } from 'src/validator'
4351
4452
export default defineComponent({
4553
name: 'Preview',
4654
setup () {
47-
const cffstr = useCffstr()
55+
const { cffstr } = useCffstr()
4856
const showTooltip = ref(false)
4957
5058
const copyToClipboard = async () => {
@@ -54,9 +62,12 @@ export default defineComponent({
5462
showTooltip.value = false
5563
}
5664
65+
const isValid = computed(isValidCffFile)
66+
5767
return {
5868
cffstr,
5969
copyToClipboard,
70+
isValid,
6071
showTooltip
6172
}
6273
}

src/store/cffstr.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,8 @@ export function useCffstr () {
7070
return yaml.dump(kebabed, { indent: 2, sortKeys: true })
7171
}
7272

73-
return computed(() => makeCffstr())
73+
return {
74+
jsObject: computed(makeJavascriptObject),
75+
cffstr: computed(makeCffstr)
76+
}
7477
}

src/validator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import Ajv from 'ajv'
22
import addFormats from 'ajv-formats'
33
import schema from './schemas/1.2.0/schema.json'
4+
import { useCffstr } from './store/cffstr'
45

56
export const ajv = new Ajv()
67
addFormats(ajv)
78
ajv.addSchema(schema)
89

10+
export const isValidCffFile = () => {
11+
const { jsObject } = useCffstr()
12+
return ajv.validate(`${schema.$id}`, jsObject.value)
13+
}
14+
915
export const makeFieldValidator = (subschema: string) => {
1016
return (val: unknown) => {
1117
const isValid = ajv.validate(`${schema.$id}#${subschema}`, val)

test/jest/__tests__/store/cffstr.jest.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useCffstr } from '../../../../src/store/cffstr'
44

55
describe('useCffstr', () => {
66
const cff = useCff()
7-
const cffstr = useCffstr()
7+
const { cffstr } = useCffstr()
88

99
beforeEach(() => {
1010
cff.reset()

0 commit comments

Comments
 (0)