Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for TypedArrays #640

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ function buildArray (context, location) {
`

functionCode += `
if (!Array.isArray(obj)) {
const supportedTypedArrays = ['Uint8Array'];
if (!Array.isArray(obj) && !(obj != null && supportedTypedArrays.indexOf(obj.constructor.name) != -1)) {
throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
}
const arrayLength = obj.length
Expand Down
28 changes: 28 additions & 0 deletions test/anyof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,31 @@ test('object with ref and validated properties', (t) => {
const stringify = build(schema, { schema: externalSchemas })
t.equal(stringify({ id: 1, reference: 'hi' }), '{"id":1,"reference":"hi"}')
})

test('anyOf with a TypedArray', (t) => {
t.plan(1)

const anyOfSchema = {
type: 'object',
properties: {
maybeArray: {
anyOf: [{
type: 'array',
items: {
type: 'number'
}
},
{
type: 'string'
}]
}
}
}
const stringify = build(anyOfSchema)

const typed = new Uint8Array(5)
typed.fill(10)

const value = stringify({ maybeArray: typed })
t.equal(value, '{maybeArray: [10,10,10,10,10]}')
})
28 changes: 28 additions & 0 deletions test/oneof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,31 @@ test('all array items does not match oneOf types', (t) => {

t.throws(() => stringify({ data: [null, false, true, undefined, [], {}] }))
})

test('oneOf with a TypedArray', (t) => {
t.plan(1)

const oneOfSchema = {
type: 'object',
properties: {
maybeArray: {
oneOf: [{
type: 'array',
items: {
type: 'number'
}
},
{
type: 'string'
}]
}
}
}
const stringify = build(oneOfSchema)

const typed = new Uint8Array(5)
typed.fill(10)

const value = stringify({ maybeArray: typed })
t.equal(value, '{maybeArray: [10,10,10,10,10]}')
})
21 changes: 21 additions & 0 deletions test/typesArray.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,24 @@ test('throw an error if none of types matches', (t) => {
const stringify = build(schema)
t.throws(() => stringify({ data: 'string' }), 'The value "string" does not match schema definition.')
})

test('typedArray Uint8Array', (t) => {
t.plan(1)
const schema = {
type: 'object',
properties: {
arr: {
type: 'array',
items: {
type: 'number'
}
}
}
}

const stringify = build(schema)
const arr = new Uint8Array(5)
arr.fill(5)

t.equal(stringify({ arr }), '{"arr":[5,5,5,5,5]}')
})