Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const validLargeArrayMechanisms = [
'json-stringify'
]

const supportedTypedArrays = [
'Uint8Array'
]

const addComma = '!addComma && (addComma = true) || (json += \',\')'

function isValidSchema (schema, name) {
Expand Down Expand Up @@ -586,7 +590,7 @@ function buildArray (context, location) {
`

functionCode += `
if (!Array.isArray(obj)) {
if (!Array.isArray(obj) && !(obj != null && (${supportedTypedArrays.map(type => ' obj.constructor.name === \'' + type + '\' ').join('||')}) )) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh. It would be better to put the array in a "global" scope and make an indexOf by the obj.constructor.name than this imho.

I think this will slow down array significantly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familiar with perf implications here.

Would Array.includes also be worth testing?

throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
}
const arrayLength = obj.length
Expand Down
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]}')
})