Skip to content

Commit b423e57

Browse files
committed
use exported Schema class where possible
1 parent 61bf10e commit b423e57

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

lib/constant.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
import { schema } from './schemas'
1+
import { v4 as uuidv4 } from 'uuid'
2+
import { Schema } from './schemas'
23

34
// Public: A constant value like a "string", number (1, 3.5), or boolean (true, false).
45
//
56
// Implements the same interface as Expression
67
export class Constant {
7-
constructor (value) {
8+
constructor (value, id = uuidv4()) {
89
this.value = value
10+
this.id = id
11+
}
12+
13+
clone (value, id = this.id) {
14+
return new Constant(value, id)
915
}
1016

1117
get args () {
1218
return [this.value]
1319
}
1420

1521
get schema () {
16-
return schema.resolve('#/definitions/constant')
22+
return Schema.resolve('#/definitions/constant')
1723
}
1824

1925
validate (schema = this.schema) {

lib/expression.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { v4 as uuidv4 } from 'uuid'
22
import { Constant } from './constant'
3-
import { schema } from './schemas'
3+
import { Schema } from './schemas'
44

55
function toArray (arg) {
66
if (Array.isArray(arg)) {
@@ -48,7 +48,7 @@ export class Expression {
4848
}
4949

5050
get schema () {
51-
return schema.resolve('#')
51+
return Schema.resolve('#')
5252
}
5353

5454
validate (schema = this.schema) {

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { schema, schemas, BaseURI } from './schemas'
2-
export { default as examples } from '../examples'
1+
export { Schema, schemas, BaseURI } from './schemas'
32
export { Expression } from './expression'
43
export { Constant } from './constant'
4+
export { default as examples } from '../examples'

test/constant.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, test, expect } from 'vitest'
2-
import { Constant, schema } from '../lib'
2+
import { Constant, Schema } from '../lib'
33

44
describe('Constant', () => {
55
describe('schema', () => {
@@ -20,13 +20,13 @@ describe('Constant', () => {
2020

2121
describe('matches', () => {
2222
test('returns true for matching validator', () => {
23-
const validator = schema.resolve('#/definitions/constant/anyOf/0')
24-
expect(new Constant('string').matches(validator)).toBe(true)
23+
const schema = Schema.resolve('#/definitions/constant/anyOf/0')
24+
expect(new Constant('string').matches(schema)).toBe(true)
2525
})
2626

2727
test('returns false for different schema', () => {
28-
const validator = schema.resolve('#/definitions/constant/anyOf/0')
29-
expect(new Constant(true).matches(validator)).toBe(false)
28+
const schema = Schema.resolve('#/definitions/constant/anyOf/0')
29+
expect(new Constant(true).matches(schema)).toBe(false)
3030
})
3131
})
3232
})

test/schemas.test.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, test, expect } from 'vitest'
2-
import { examples, schema, Expression } from '../lib'
2+
import { examples, Schema, Expression } from '../lib'
33

44
describe('schema.json', () => {
55
for (const [name, example] of Object.entries(examples)) {
@@ -36,38 +36,39 @@ describe('schema.json', () => {
3636

3737
describe('resolve', () => {
3838
test('returns a schema', () => {
39-
const ref = schema.resolve('#/definitions/constant')
39+
const ref = Schema.resolve('#/definitions/constant')
4040
expect(ref.title).toEqual('Constant')
4141
expect(ref.validate(true)).toEqual({ valid: true, errors: null })
4242
})
4343

4444
test('resolves refs', () => {
45-
expect(schema.resolve('#/definitions/function/properties/Any').title).toEqual('Any')
46-
expect(schema.definitions.function.properties.Any.title).toEqual('Any')
45+
expect(Schema.resolve('#/definitions/function/properties/Any').title).toEqual('Any')
46+
expect(Schema.resolve('#').definitions.function.properties.Any.title).toEqual('Any')
4747
})
4848
})
4949

5050
describe('resolveAnyOf', () => {
5151
test('returns nested anyOf', () => {
52-
expect(schema.resolveAnyOf()).toHaveLength(4)
52+
const ref = Schema.resolve('#')
53+
expect(ref.resolveAnyOf()).toHaveLength(4)
5354
})
5455

5556
test('returns array of schemas', () => {
56-
const ref = schema.resolve('#/definitions/constant')
57+
const ref = Schema.resolve('#/definitions/constant')
5758
expect(ref.resolveAnyOf()).toHaveLength(3)
5859
expect(ref.resolveAnyOf()).toEqual(ref.anyOf)
5960
})
6061
})
6162

6263
describe('arrayItem', () => {
6364
test('returns schema for repeated array item', () => {
64-
const any = schema.resolve("Any.schema.json")
65+
const any = Schema.resolve('Any.schema.json')
6566
expect(any.arrayItem(0).title).toEqual('Expression')
6667
expect(any.arrayItem(99).title).toEqual('Expression')
6768
})
6869

6970
test('returns schema for tuple', () => {
70-
const duration = schema.resolve("Duration.schema.json")
71+
const duration = Schema.resolve('Duration.schema.json')
7172
expect(duration.arrayItem(0).title).toEqual('Number')
7273
expect(duration.arrayItem(1).title).toEqual('Unit')
7374
expect(duration.arrayItem(2)).toBe(undefined)

0 commit comments

Comments
 (0)